【问题标题】:2-column TableLayout with 50% exactly for each column2 列 TableLayout,每列准确占 50%
【发布时间】:2011-01-18 14:10:05
【问题描述】:

自从我第一次接触 Android 以来,这个问题一直困扰着我。我不能使两列 TableLayout 中的两列各精确到 50%。

这是一个例子:

<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 而变得一团糟。

我的定义有什么问题?

提前非常感谢。 哈杰维

【问题讨论】:

    标签: android layout resources


    【解决方案1】:

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

    当我也想在两个子视图之间平均分配给定空间时,我遇到过类似的情况,但由于具有“更广泛内容”的子视图在其父视图中也比其兄弟视图更宽,因此未能这样做。这是因为较宽的孩子具有较大的初始宽度。确保两个孩子从相同的宽度开始(android:layout_width="0dp" 在他们两个上)也保证在他们之间平均分配空间。

    【讨论】:

    • 更重要的是,如果你这样做,转储TableLayout 并切换到LinearLayoutTableLayout 专为需要 Android 根据单元格内容计算列宽的情况而设计。如果您故意忽略这一点,您不妨改用嵌套的LinearLayouts
    • @ChristianB:感谢您的解释。我一直认为 TableLayout 一定有什么“错误”。有趣的是,许多失败的 TableLayout 与 2.3 模拟器完美配合。 @dbm:将 layout_width 设置为 0dp 所有子视图(TextView/EditText)都消失了。不管怎么说,还是要谢谢你。我将检查 CommonsWares 的解决方案并完全删除 TableLayouts。不知道它们有什么用,如果您需要知道它们可能会打印什么并且可能会弄乱它们......
    • @CommonsWare:无法相信我为什么选择 TableLayouts?将它们更改为 LinearLayout 效果很好。非常感谢。
    【解决方案2】:

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

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

    【讨论】:

    • android:stretchColumns="0,1" 在这种情况下可以只替换为 android:stretchColumns="*"
    【解决方案3】:

    我会创建一个表格布局 在控制您的框架布局内。在这个例子中,我有两列布局,我将两个 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>
    

    【讨论】:

    • 这对我有用。不过,我确实必须将 center_vertical 添加到 FrameLayout 元素中。
    【解决方案4】:

    android 表中的列始终调整为任何行中最宽的视图。这里没有对列的显式控制。网格视图对列的大小有更多的控制如:android:columnWidth="100dp"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2014-01-05
      • 2019-01-05
      相关资源
      最近更新 更多