【问题标题】:How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?如何在 pre API 21 中支持 `layout_columnWeight` 和 `layout_rowWeight`?
【发布时间】:2017-02-18 15:34:02
【问题描述】:

我使用下面的网格布局和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 及更高版本。 pre API 21 如何支持layout_columnWeightlayout_rowWeight 功能?

【问题讨论】:

    标签: android android-gridlayout


    【解决方案1】:

    支持库中的 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>
    

    【讨论】:

    • 我在 xml 中的各种属性上遇到了错误的 namspace 错误:(
    • 您是否将库添加到 build.gradle 文件中?
    • 添加了compile 'com.android.support:gridlayout-v7:24.2.1'
    • 刚刚更新了我写的布局。某些属性的命名空间有问题;)
    • 不工作:(它将整个而不是100x100dp。
    【解决方案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>
    

    【讨论】:

      【解决方案3】:

      而不是使用

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

      用途:

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

      这会有所帮助。

      【讨论】:

      • 请争论为什么在其他答案中更喜欢这种方法而不是“支持方法”。
      • 应用程序:layout_columnWeight) 未找到
      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多