2列TableLayout,每列精确50%

Har*_*elm 33 resources layout android

自从我使用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>
Run Code Online (Sandbox Code Playgroud)

这是相应的样式定义:

<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>
Run Code Online (Sandbox Code Playgroud)

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

我的定义有什么问题?

提前谢谢了.HJW

dbm*_*dbm 67

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

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

  • 更重要的是,如果你这样做,转储`TableLayout`并切换到`LinearLayout`.`TableLayout`适用于需要Android根据单元格内容计算列宽的情况.如果你故意忽略它,你也可以使用嵌套的`LinearLayouts`. (12认同)
  • @CommonsWare:不敢相信我为什么选择TableLayouts?将它们更改为LinearLayout非常有效.非常感谢你. (3认同)

out*_*ast 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> 
Run Code Online (Sandbox Code Playgroud)


Edm*_*ake 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>
Run Code Online (Sandbox Code Playgroud)