Подтвердить что ты не робот

Как поддерживать "layout_columnWeight" и "layout_rowWeight" в pre API 21?

Я использую приведенную ниже схему сетки с layout_columnWeight и layout_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 и выше. Как поддерживать layout_columnWeight и layout_rowWeight в pre API 21?

4b9b3361

Ответ 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;)

используйте его, как android.support.v7.widget.GridLayout ниже (android.support.v7.widget.GridLayout) в вашем XML файле макета:

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

Ответ 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>