如何在GridLayout中为低于21

时间:2016-01-09 14:04:59

标签: android user-interface android-support-library android-gridlayout

我想使用GridLayout均匀地传播8个按钮。我想支持低于21的API(这是layout_columnWeight中支持layout_rowWeightGridLayout的最小数量

这是我的代码

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="tk.adamnafie.basicphrases.MainActivity"
android:background="@drawable/background">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_column="0"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button1"
        android:onClick="talk"
        android:text="Good Morning"
        android:layout_width="150dp" />

    <Button
        android:layout_column="1"
        android:layout_row="0"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button2"
        android:onClick="talk"
        android:text="Good Afternoon"
        android:layout_width="150dp" />

    <Button
        android:layout_column="0"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button3"
        android:onClick="talk"
        android:text="Good night"
        android:layout_width="150dp" />

    <Button
        android:layout_column="1"
        android:layout_row="1"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button4"
        android:onClick="talk"
        android:text="How are you?"
        android:layout_width="150dp" />

    <Button
        android:layout_column="0"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button5"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="Can you talk English?" />

    <Button
        android:layout_column="1"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button6"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="I am learning German" />

    <Button
        android:layout_column="0"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button7"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="Where do you live?" />

    <Button
        android:layout_column="1"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:layout_gravity="fill"
        android:id="@+id/button8"
        android:onClick="talk"
        android:layout_width="150dp"
        android:text="I come from Egypt" />

    </GridLayout>
</RelativeLayout>

我想支持Android 4.0作为最低SDK。有没有简单的方法呢?

1 个答案:

答案 0 :(得分:17)

您可以使用支持库版本的GridView,它将功能反向移植到早期的Android版本。在build.gradle(模块)中,您需要以下依赖项:

compile "com.android.support:support-v4:23.1.1"
compile "com.android.support:gridlayout-v7:23.1.1"

然后在xml布局文件中使用:

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        app:layout_column="0"
        app:layout_row="0"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_gravity="fill"
        android:id="@+id/button1"
        android:onClick="talk"
        android:text="Good Morning"
        android:layout_width="150dp" />

您还需要将app命名空间添加到包含的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"