2列TableLayout,每列精确50%

时间:2011-01-18 14:10:06

标签: android layout resources

自从我使用Android的第一步以来,这个让我感到困惑。我不能在两列TableLayout中使两列完全相同。

以下是一个例子:

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

    <TableLayout
        android:id="@+id/tablelayout"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:paddingRight="2dip" >

        <TableRow>
            <TextView
                style="@style/TextViewStandard"
                android_layout_span="2"
                android:layout_weight="1" 
                android:text="Bla" />
        </TableRow>

        <TableRow>
            <TextView
                style="@style/TextViewStandard"
                android:layout_weight="1"
                android:text="Name:" />

            <EditText
                style="@style/EditTextStandard"
                android:id="@+id/et_name"
                android:layout_weight="1" />
        </TableRow>

        <TableRow>
            <TextView
                style="@style/TextViewStandard"
                android:layout_weight="1"
                android:text="URL:" />

            <EditText
                style="@style/EditTextStandard"
                android:id="@+id/et_url"
                android:layout_weight="1" />
        </TableRow>

        <TableRow>
            <Button
                style="@style/ButtonStandard"
                android:layout_column="0"
                android:layout_marginTop="6dip"
                android:onClick="onClickOk"
                android:text="@android:string/ok" />
        </TableRow>
    </TableLayout>
</ScrollView>

这是相应的样式定义:

<resources>
    <style name="ButtonStandard" parent="@android:style/Widget.Button">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">fill_parent</item>
    </style>

    <style name="EditTextStandard" parent="@android:style/Widget.EditText">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">2dip</item>
        <item name="android:layout_marginRight">2dip</item>
        <item name="android:layout_marginTop">2dip</item>
        <item name="android:layout_width">fill_parent</item>
    </style>

    <style name="TextViewStandard" parent="@android:style/Widget.TextView">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">2dip</item>
        <item name="android:layout_marginRight">2dip</item>
        <item name="android:layout_marginTop">2dip</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:textColor">@android:color/white</item>
    </style>
</resources>

这变得非常混乱,涉及CheckBox。

我的定义有什么问题?

非常感谢提前。 HJW

4 个答案:

答案 0 :(得分:66)

您是否尝试将android:layout_width属性设置为0dp(当然将android:layout_weight设为1)在子视图上?

当我也希望在两个子视图之间平均分配给定空间但我没有这样做,因为具有“更宽内容”的孩子在其父级内部变得比其兄弟姐妹更宽。这是因为较宽的孩子的初始宽度较大。确保两个孩子从相同的宽度开始(两个android:layout_width="0dp")也保证在它们之间均匀分配空间。

答案 1 :(得分:27)

这就是我如何均匀地格式化表格。不是你的问题的一部分,而是跨越所有列android:layout_span =“2”在不包含android:layout_column属性的其他TableRow子项上。希望这会有所帮助。

<TableLayout 
  android:id="@+id/tableLayout1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0,1">

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

        <TextView
          android:id="@+id/textView1"
          android:layout_column="0">
        </TextView>

        <TextView
          android:id="@+id/textView2"
          android:layout_column="1">
        </TextView>

    </TableRow>

</TableLayout> 

答案 2 :(得分:17)

我会创建一个表格布局 在一个框架布局内举行你的控制。在这个例子中,我有两列布局,我把两个ImageView放在每列的中心。

    <TableLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="1">

        <TableRow>
            <FrameLayout 
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1">

                <ImageView android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:src="@drawable/button_wedding_day_cheat_sheet" 
                    android:id="@+id/buttonWeddingDayCheatSheet"
                    android:onClick="onClickWeddingDayCheatSheet"
                    android:layout_gravity="center_horizontal">
                </ImageView>
            </FrameLayout>

            <FrameLayout 
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1">
                <ImageView android:layout_width="wrap_content" 
                    android:layout_height="wrap_content" 
                    android:src="@drawable/button_share_favorite_recipe" 
                    android:id="@+id/buttonShareFavoriteRecipe"
                    android:onClick="onClickShareFavoriteRecipe"
                    android:layout_gravity="center_horizontal">
                </ImageView>
                </FrameLayout>
            </TableRow>

    </TableLayout>

答案 3 :(得分:0)

android表中的列始终适应任何行中最宽的视图。列中没有明确的控件。网格视图可以更好地控制列大小,例如:android:columnWidth =“100dp”

如果表格中的左视图和右视图设置为相同的大小,则居中的表格可以有两个50%间隔的列。