简体   繁体   English

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

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

I use the below grid layout with layout_columnWeight and layout_rowWeight to centralize my view in the grid cell.我使用带有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>

But they are for v21 and above only.但它们仅适用于 v21 及更高版本。 How to support layout_columnWeight and layout_rowWeight feature in pre API 21?如何在 API 21 之前支持layout_columnWeightlayout_rowWeight功能?

the version of GridLayout in support library is backward compatible and supports weights as mentioned here:支持库中的 GridLayout 版本向后兼容并支持这里提到的权重:

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

so you just need to add compile 'com.android.support:gridlayout-v7:23.1.1' to your build.gradle file and use support gridlayout instead ;)所以你只需要在你的 build.gradle 文件中添加compile 'com.android.support:gridlayout-v7:23.1.1'并使用 support gridlayout ;)

use it like below ( android.support.v7.widget.GridLayout ) in your layout xml file:在您的布局 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>

For beginners like me @Amir Ziarati's answer is on point.对于像我这样的初学者来说, @Amir Ziarati 的答案是正确的。 you need to add this line in your build.gradle (Module:app)您需要在build.gradle (Module:app) 中添加这一行

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

Like this像这样

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

And then use然后使用

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

Instead of Using而不是使用

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

Use:使用:

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

This will help.这会有所帮助。

暂无
暂无

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

相关问题 如何以编程方式将app:layout_columnWeight和app:layout_rowWeight设置为GridLayout的子级 - how to set app:layout_columnWeight and app:layout_rowWeight to GridLayout's child programmatically 如何在样式中使用支持 layout_columnWeight? - How to use support layout_columnWeight in a style? 如何以编程方式将 android:layout_columnWeight="1" 设置为 android 支持 v7 Gridlayout 中的元素 - How do I set android:layout_columnWeight="1" programmatically to an element in an android support v7 Gridlayout 如何从代码中设置“ app:layout_columnWeight”? - how set “app:layout_columnWeight” from code? 如何以编程方式添加GridLayout子项的layout_columnWeight? - How to set layout_columnWeight for a GridLayout child when added programmatically? Android GridLayout:layout_rowWeight 没有平均划分项目宽度 - Android GridLayout: layout_rowWeight not dividing item width equally 在 Gridlayout 中的 MaterialCardView 中找不到属性 layout_columnWeight - Attribute layout_columnWeight not found in MaterialCardView inside Gridlayout 在包“android”中找不到属性“layout_columnWeight”的资源标识符 - No resource identifier found for attribute 'layout_columnWeight' in package 'android' Android GridLayout layout_columnWeight="1" 和 layout_width="0dp" 以编程方式 - Android GridLayout layout_columnWeight="1" and layout_width="0dp" programmatically Android API 21缺少包含的布局 - Included layout missing on Android API 21
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM