如何在API 21之前的版本中支持`layout_columnWeight`和`layout_rowWeight`? [英] How to support `layout_columnWeight` and `layout_rowWeight` in pre API 21?

查看:120
本文介绍了如何在API 21之前的版本中支持`layout_columnWeight`和`layout_rowWeight`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将下面的网格布局与layout_columnWeightlayout_rowWeight一起使用,以将视图集中在网格单元格中.

I use the below grid layout with layout_columnWeight and layout_rowWeight to centralize my view in the grid cell.

<?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功能?

But they are for v21 and above only. How to support layout_columnWeight and layout_rowWeight feature in pre API 21?

推荐答案

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

the version of GridLayout in support library is backward compatible and supports weights as mentioned here:

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

所以您只需要在您的build.gradle文件中添加compile 'com.android.support:gridlayout-v7:23.1.1'并使用support gridlayout代替;)

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

在布局xml文件中像下面(android.support.v7.widget.GridLayout)一样使用它:

use it like below (android.support.v7.widget.GridLayout) in your layout xml file:

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

这篇关于如何在API 21之前的版本中支持`layout_columnWeight`和`layout_rowWeight`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆