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>

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

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

4

3 回答 3

33

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

  <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> 
于 2013-05-23T13:08:04.450 回答
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 的权重单元格,你永远不会得到一半的屏幕,因为第一个单元格不能小于第三个单元格上的内容。
于 2014-11-10T14:18:44.283 回答
-1

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

于 2018-06-07T14:31:11.493 回答