Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 布局宽度=匹配父级的TableLayout不匹配父级_Android_Android Layout_Android Tablelayout - Fatal编程技术网

Android 布局宽度=匹配父级的TableLayout不匹配父级

Android 布局宽度=匹配父级的TableLayout不匹配父级,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我有一个包含两列和两行的tableLayout,两行和最后一列的宽度都与父项匹配,但布局没有填充父项宽度,它的组成就像它包含wrap\u内容一样 代码如下: <TableLayout android:layout_width="match_parent"> <TableRow android:layout_width="match_parent"> <TextView android:layout_width="wr

我有一个包含两列和两行的tableLayout,两行和最后一列的宽度都与父项匹配,但布局没有填充父项宽度,它的组成就像它包含wrap\u内容一样

代码如下:

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

对于每一行具有父行宽度的行,我需要做什么


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

请尝试此代码,我认为它将帮助您:

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


>P>还考虑是否有其他行的内容比包含TeXVIEW的其他行更宽,因为权重属性不能正常工作。考虑这个例子:

<?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,因为第一个单元格不能小于第三个单元格上的内容,所以你永远不会看到一半的屏幕

我会尝试在TableRow中添加
android:weight=“1”

在我的
TableRow中,android:layout\u weight=“1”
帮了我的忙。谢谢