如何在 android 中使用 Grid 布局将屏幕分成 4 等份?
How to Divide screen into 4 equal part using Grid layout in android?
我曾尝试将屏幕分成 4 个相等的部分,但遇到了问题。
<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 文件是
<?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>
现在矩形在屏幕外,第一列填满整个屏幕。
您可以使用 LinearLayout.Below 是 4 个按钮的示例。
<?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>
开始 API 21 您可以在 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
如果您尝试动态获取 GridLayout,即从网络服务获取。它不能正常工作。您应该使用 Getter Setter 尝试 GridView,它会工作正常。不要尝试没有 getter setter。否则会有一些关于项目位置的问题。
我曾尝试将屏幕分成 4 个相等的部分,但遇到了问题。
<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 文件是
<?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>
现在矩形在屏幕外,第一列填满整个屏幕。
您可以使用 LinearLayout.Below 是 4 个按钮的示例。
<?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>
开始 API 21 您可以在 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如果您尝试动态获取 GridLayout,即从网络服务获取。它不能正常工作。您应该使用 Getter Setter 尝试 GridView,它会工作正常。不要尝试没有 getter setter。否则会有一些关于项目位置的问题。