如何在android中使用网格布局将屏幕分成4等分? [英] How to Divide screen into 4 equal part using Grid layout in android?

查看:65
本文介绍了如何在android中使用网格布局将屏幕分成4等分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试将屏幕分成 4 个相等的部分,但遇到了问题.

I have try to divide the screen into 4 equal parts but got problem.

   <GridLayout
       android:rowCount="2"
       android:columnCount="2"
       android:layout_width="match_parent"
       android:layout_height="match_parent">


       <View
           android:background="@drawable/rectangle"
           android:layout_column="0"
           android:layout_row="0"
           />


       <View
           android:background="@drawable/rectangle"
           android:layout_column="1"
           android:layout_row="0"
           />

       <View
           android:background="@drawable/rectangle"
           android:layout_column="0"
           android:layout_row="1"
           />

          <View
           android:background="@drawable/rectangle"
           android:layout_column="1"
           android:layout_row="1"
           />

和 rectangle.xml 文件是

and the the rectangle.xml file is

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_background_shape">
    <stroke android:width="2dp" android:color="#ff207d94" />
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="20dp" />
    <solid android:color="#ffffffff" />
</shape>

现在矩形超出屏幕,第一列填满整个屏幕.

Right now rectangle are going outside of screen and first column filling the whole screen.

推荐答案

从 API 21 开始,您可以在 GridLayout 中使用权重:

Starting API 21 you can use weights in GridLayout:

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="2">

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/rectangle"/>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/rectangle"/>

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/rectangle" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/rectangle" />

</GridLayout>

如果需要支持以前的api,请使用android.support.v7.widget.GridLayout

Use android.support.v7.widget.GridLayout if you need to support previous apis

这篇关于如何在android中使用网格布局将屏幕分成4等分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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