无法显示Gridlayout顶部和底部边距

时间:2018-01-26 22:18:53

标签: android android-layout android-gridlayout

我尝试在linearlayout(表格)中显示gridlayouts(作为行)

<ScrollView android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical">

        <LinearLayout 
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >
        <!--
        <include layout="@layout/row"></include>
        <include layout="@layout/sep"></include>
        <include layout="@layout/row"></include> 
        -->
    </LinearLayout>

</ScrollView>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/row"
     android:layout_marginBottom="10dp" <-- problem -->
     android:layout_marginTop="10dp"    <-- problem -->
     android:layout_width="match_parent"
     android:layout_height="40dp"
     android:columnCount="2"
     android:rowCount="2"
     android:orientation="horizontal"
     android:background="@color/orange">
    ...
 </GridLayout>

sep.xml

<View 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000"
/>

在模拟器和手机中:当我添加一行,一个sep(arator),一行等时,我有两个问题:

  • 不显示分隔符行(即使我增加了layout_height)
  • 不考虑行的顶部和底部边距(gridlayout)。

我只获得了@color/orange背景色的完整行块。

注1:当我取消注释“包含布局”块时,它在预览中正确显示。

注2:我必须使用GridLayout,因为我需要合并行和列。

我的想法已经不多了。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,最后是因为android:layout_marginTopandroid:layout_marginBottom标记仅由API 26处理,而我的 targetSdkVersion 在gradle文件中为24。

布局编辑器不显示任何警告。我必须悬停属性才能看到它。

因此,为了绕过这个限制,我使用类似的方式以编程方式完成:

LinearLayout.MarginLayoutParams lp = (LinearLayout.MarginLayoutParams) row.getLayoutParams();
lp.bottomMargin = lp.topMargin = Utils.getIntFromDP(10, getResources());
row.setLayoutParams(lp);