【问题标题】:Setting layout_marginBottom for TableLayout inside LinearLayout has no effect在 LinearLayout 中为 TableLayout 设置 layout_marginBottom 无效
【发布时间】:2012-07-23 02:26:31
【问题描述】:

我有一个垂直的 LinearLayout (绿色矩形),我正在尝试以编程方式将多个 TableLayouts (红色矩形) 膨胀并插入到此 LinearLayout 中。每个表格布局包含 4 个 2x2 排列的单元格。

除了我为 TableLayout 设置的任何 layout_marginBottom 都不起作用之外,它可以工作,红色矩形总是紧紧地挤在 LinearLayout 内。我想在每个表格之间创建一些垂直间距。

请提出我的 XML 有什么问题:

线性布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/levellist"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="vertical">

    </LinearLayout>
</ScrollView>

TableLayout(动态插入)。注意没有效果的 layout_marginBottom :

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="100dp"
    android:stretchColumns="*" >

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

        <include
            android:id="@+id/levelview0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            layout="@layout/levelmenu_level" />

        <include
            android:id="@+id/levelview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            layout="@layout/levelmenu_level" />
    </TableRow>

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

        <include
            android:id="@+id/levelview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            layout="@layout/levelmenu_level" />

        <include
            android:id="@+id/levelview3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            layout="@layout/levelmenu_level" />
    </TableRow>

</TableLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    在这个 TableLayout 中有一堆相同的视图。我建议尝试使用 GridLayout。它应该有助于内存管理和回收。但要回答您的问题,您似乎正在为整个表格设置边距,而不是每一行。您是否尝试过将 layout_margin 放在行标签内?您也可以尝试设置填充?这样,您就可以将间距放在行内。但是,我相信无论哪种方式,您都应该获得相同的效果。边距在视图之外,填充在内部。

    http://developer.android.com/reference/android/view/View.html#attr_android:paddingBottom

    <TableRow
            android:id="@+id/TableRow01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="???dip">
    
            <include
                android:id="@+id/levelview0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                layout="@layout/levelmenu_level" />
    

    【讨论】:

    • 是的,我想设置每个表之间的边距,而不是每行之间。我想使用边距而不是填充,因为我计划为表格使用背景图像,并且我希望在一个背景开始和另一个背景开始之间有空白。我只是好奇为什么我不能在表格底部设置边距。
    • 您希望在每个“级别”之间进行填充。你有一个大的表格布局,并且在表格的行中包含每个级别。所以将你的间距移动到这些行的标签中。
    • 抱歉,感谢您的帮助,但我认为您误解了我的意思。不,我没有一个大的表格布局。我有一个大的线性布局。线性布局包含多个表格(图像中的每个红色矩形都是一个表格)。每个表包含 4 个单元格。我想在每个表(而不是每一行)之间创建一个间距。它可以改成一张大桌子,但这不是我要问的具体问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2017-07-28
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2014-08-07
    相关资源
    最近更新 更多