How to Divide screen into 4 equal part using Grid layout in android?

耗尽温柔 提交于 2020-04-21 05:32:51

问题


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

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.


回答1:


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>

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




回答2:


You can do it with LinearLayout.Below is a example of 4 buttons.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"

    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:layout_weight="1.0"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_light"
        android:layout_weight="1.0"
        android:text="Button" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light"
        android:layout_weight="1.0"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_light"
        android:layout_weight="1.0"
        android:text="Button" />

</LinearLayout>
</LinearLayout>



回答3:


If you are trying to get GridLayout Dynamically i.e. from web-services. it doesn't work fine. You should try GridView using Getter Setter, it would work fine. Don't try without getter setter. Otherwise there would be some issues regarding to item's position.



来源:https://stackoverflow.com/questions/39685060/how-to-divide-screen-into-4-equal-part-using-grid-layout-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!