繁体   English   中英

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

[英]How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?

我使用带有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功能?

支持库中的 GridLayout 版本向后兼容并支持这里提到的权重:

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

所以你只需要在你的 build.gradle 文件中添加compile 'com.android.support:gridlayout-v7:23.1.1'并使用 support 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>

对于像我这样的初学者来说, @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>

而不是使用

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

使用:

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

这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM