TextView在表格布局中无法正确显示

时间:2013-12-14 05:35:12

标签: android android-layout textview android-tablelayout

textview未正确显示文本。文本跨越两行,但第二行文本仅显示文本的上半部分,就好像文本被水平切割成两半,尽管使用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向下移动到与按钮基线对齐。无法更改此行为以保留现有应用程序中的布局。在您的案例中实施此布局的正确方法是在android:baselineAligned="false"标记上添加LinearLayout。这也将消除TextView上方的额外垂直空间。

4 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

试试这个 -

    <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/

答案 2 :(得分:0)

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

由于重量,因此在渲染时计算宽度。

因此,在指定宽度时,textview的高度已设置。

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

答案 3 :(得分:0)

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