如何在API 21之前支持`layout_columnWeight`和`layout_rowWeight`?

17

我使用以下带有layout_columnWeightlayout_rowWeight的网格布局,以使我的视图在网格单元格中居中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<GridLayout
    android:id="@+id/container_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="3"
    android:orientation="horizontal">

    <View
        android:id="@+id/view_red"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="center"
        android:background="#ff0000"
        android:layout_row="0"
        android:layout_column="0" />

    <View
        android:id="@+id/view_green"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="center"
        android:background="#00ff00"
        android:layout_row="0"
        android:layout_column="0" />

    <View
        android:id="@+id/view_blue"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="center"
        android:background="#0000ff"
        android:layout_row="0"
        android:layout_column="0" />
    </GridLayout>
</RelativeLayout>

但是它们仅适用于v21及以上版本。如何在API 21之前支持layout_columnWeightlayout_rowWeight功能?

3个回答

24

支持库中的 GridLayout 版本向后兼容,并支持按照此处所述的权重:

https://developer.android.com/reference/android/support/v7/widget/GridLayout.html

因此,您只需将compile 'com.android.support:gridlayout-v7:23.1.1'添加到您的 build.gradle 文件中,并使用支持的 GridLayout 代替即可 ;)

在您的布局 xml 文件中使用如下代码(android.support.v7.widget.GridLayout):

<android.support.v7.widget.GridLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:columnCount="2"
    app:rowCount="3"
    app:orientation="horizontal">

    <View
        android:id="@+id/view_red"
        android:layout_height="100dp"
        android:layout_width="100dp"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_gravity="center"
        android:background="#ff0000"
        app:layout_row="0"
        app:layout_column="0" />
</android.support.v7.widget.GridLayout>

你在 build.gradle 文件中添加了库吗? - Amir Ziarati
添加了 compile 'com.android.support:gridlayout-v7:24.2.1' - Elye
我刚刚更新了我写的布局。一些属性的命名空间有问题 ;) - Amir Ziarati
它适用于一个单元格的情况,但当我添加另一个视图时,它不显示第二列或行。 - Elye
你是否也在其他单元格中使用了 app 命名空间? - Amir Ziarati
显示剩余8条评论

2

使用替代方案

android:layout_columnWeight="1"
android:layout_rowWeight="1"

使用:

app:layout_columnWeight="1"
app:layout_rowWeight="1"

这将有所帮助。

请阐述为什么要选择这种方法而不是其他答案中的“支持方法”。 - greybeard
找不到app:layout_columnWeight。 - Emmanuel Njorodongo

2

对于像我这样的初学者,@Amir Ziarati的回答很到位。您需要在您的build.gradle(Module:app)中添加此行。

implementation 'com.android.support:gridlayout-v7:26.1.0'

像这样

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:gridlayout-v7:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' 
}

然后使用

<android.support.v7.widget.GridLayout>
...</android.support.v7.widget.GridLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接