【问题标题】:TextView not appearing correctly in Table layoutTextView 在表格布局中未正确显示
【发布时间】:2013-12-20 18:35:59
【问题描述】:

文本视图未正确显示文本。文本跨越两行,但第二行文本只显示文本的上半部分,就好像文本被水平切成两半一样,尽管使用了height = wrap_content

xml代码:

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="15dp"
        android:weightSum="1" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:text="No. of direct Reportees:" />

        <EditText
            android:id="@+id/direct"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:hint="Eg. 12"
            android:inputType="numberDecimal"
            android:textAppearance="?android:attr/textAppearanceSmall" >

            <requestFocus />
        </EditText>
    </TableRow>

已解决:

此行为是由于基线对齐。容器具有正确的高度(它是它的两个孩子的最大值),但 Textview 向下移动以与按钮对齐基线。无法更改此行为以保留现有应用程序中的布局。在您的情况下实现此布局的正确方法是在LinearLayout 标签上添加android:baselineAligned="false"。这也将消除TextView上方的额外垂直空间。

【问题讨论】:

    标签: android android-layout textview android-tablelayout


    【解决方案1】:

    TextViewEditText 放在LinearLayout 中以使权重属性起作用:

    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="15dp">
    <LinearLayout
            android:id="@+id/table_row_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.4"
                android:text="No. of direct Reportees:" />
    
            <EditText
                android:id="@+id/direct"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.6"
                android:hint="Eg. 12"
                android:inputType="numberDecimal"
                android:textAppearance="?android:attr/textAppearanceSmall" >
    
                <requestFocus />
            </EditText>
    </LinearLayout>
    </TableRow>
    

    【讨论】:

    • 出现错误:可疑大小:这将使视图不可见,可能用于 layout_height
    • 你不应该在 wrap_content 中有 match_parent。同时,我认为重量也适用于表格行。
    • 完全正确。高度是这里的问题
    【解决方案2】:

    我认为高度是在宽度之前分配给文本视图的。

    由于重量,宽度是在渲染时计算的。

    所以在分配宽度的时候,textview 的高度已经设置好了。

    我认为如果该文本视图中的文本将是静态的,请尝试给它一些硬编码的高度。

    【讨论】:

      【解决方案3】:

      试试这个-

          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.4"
              android:text="No. of direct Reportees:" />
      
          <EditText
              android:id="@+id/direct"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="0.6"
              android:hint="Eg. 12"
              android:inputType="numberDecimal"
              android:textAppearance="?android:attr/textAppearanceSmall" >
      
              <requestFocus />
          </EditText>
      </TableRow>
      

      来源-

      http://www.chess-ix.com/blog/the-use-of-layout_weight-with-android-layouts/

      【讨论】:

      • 我需要权重...无法删除
      【解决方案4】:

      此行为是由于基线对齐。容器具有正确的高度(它是其两个子项的最大值),但 Textview 向下移动以与按钮对齐基线。无法更改此行为以保留现有应用程序中的布局。在您的情况下实现此布局的正确方法是在 LinearLayout 标签上添加 android:baselineAligned="false" 。这也将摆脱 TextView 上方的额外垂直空间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多