[英]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"
/>
而rectangular.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>
現在,矩形超出了屏幕范圍,第一列填充了整個屏幕。
從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
您可以使用LinearLayout做到這一點。下面是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>
如果您嘗試動態獲取GridLayout,即從Web服務獲取。 它不能正常工作。 您應該使用Getter Setter嘗試GridView ,它將正常工作。 不要在沒有getter setter的情況下嘗試。 否則,將有一些關於物品位置的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.