【问题标题】:TableLayout with layout_width=matchparent not matching parent具有 layout_width=matchparent 的 TableLayout 不匹配父级
【发布时间】:2013-05-23 12:48:44
【问题描述】:

我有一个包含两列和两行的 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>

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

Ps:我工作的地方不允许我发布我的代码,所以我写的代码尽可能接近我的代码。不知道对不对,无法测试。

【问题讨论】:

    标签: android android-layout android-tablelayout


    【解决方案1】:

    试试这个代码,我想它会帮助你:

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

    【讨论】:

    • android:layout_weight="1" 在我的TableRow 中为我解决了问题。谢谢!
    【解决方案2】:

    还要考虑是否有其他行的内容比包含 TextView 的其他行宽,因为 weight 属性不会很好地工作。考虑这个例子:

    <?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对于正确的单元格,您永远不会得到一半的屏幕,因为第一个单元格不能小于第三个单元格上的内容。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多