TableLayout的layout_width设置为match_parent时无法匹配父容器

13
我有一个tableLayout,有两列和两行。所有行和最后一列的宽度都设置为match_parent,但是布局没有填满父容器的宽度,表现得像它的宽度是wrap_content。
以下是代码:
<TableLayout android:layout_width="match_parent">
    <TableRow android:layout_width="match_parent">
        <TextView 
            android:layout_width="wrap_content"
            android:text="static" />
        <EditText
            android:layout_width="match_parent"
            android:text="text" />
    </TableRow>

    <TableRow android:layout_width="match_parent">
        <TextView 
            android:layout_width="wrap_content"
            android:text="static2" />
        <EditText
            android:layout_width="match_parent"
            android:text="text2" />
    </TableRow>
</TableLayout>

每一行的宽度都需要与其父元素相同,我该怎么做?

注:我的工作地点不允许我发布代码,所以我尽可能接近我的代码编写了一段代码。我不知道它是否正确,因为我无法测试。

3个回答

32

试试这段代码,我认为它会对你有帮助:

  <TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    >

    <TableRow android:layout_width="match_parent" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:text="static" />

        <EditText
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="text" />
    </TableRow>

    <TableRow android:layout_width="match_parent" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_weight="1"
            android:text="static2" />

        <EditText
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="text2" />
    </TableRow>
</TableLayout> 

8
在我的TableRow中,android:layout_weight="1"解决了我的问题。谢谢! - Redson

2

同时考虑是否有其他行的内容比包含TextView的其他行更宽,因为权重属性可能无法正常工作。考虑以下示例:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

             <TextView
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Short Text" />

              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Short Text"
            android:layout_weight="1"/>

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Reaaaaaaaaaaaaaaaaaaaaaaaaaally lengthy and text"/>

    </TableRow>

所以这是结果:

  • 第三行单元格设置了每个左侧单元格的宽度。
  • 第一行中的EditText从左侧单元格结束的地方开始(几乎占据屏幕宽度的四分之三)。
  • 第二行的每个单元格都被赋予权重1,因此该行中的两个单元格应该测量屏幕的一半,但由于第一个单元格是那么宽,权重按比例计算,因此即使您为右侧单元格设置50的权重,您也永远不会获得屏幕的一半,因为第一个单元格不能小于第三个单元格上的内容。

-1

我建议在TableRow中添加android:weight="1"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接