[英]How to Divide screen into 4 equal part using Grid layout in android?
[英]divide screen into 4 equal parts - android
您只能使用LinearLayouts和權重來做到這一點:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:padding="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#ff0000"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:background="#ff8000"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#336699"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#993366"/>
</LinearLayout>
</LinearLayout>
更新:在塊之間和塊之間有10dp“空白”。 如果您還想對每個塊應用圓角,如屏幕截圖所示,請查看android的xml drawables。 然后可以將它們應用於android:background
而不是像我的示例中那樣的純色;)
更新2:使用xml drawable創建圓角:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="10dp" />
<solid
android:color="#ff8000" />
</shape>
就像將任何其他圖像(png / jpg / etc)一樣,將xml drawable保存到您的drawable目錄中,並從視圖中引用它,例如:
android:background="@drawable/your_xml_drawable_res"
當然,如果您對所有4個正方形應用與背景相同的資源,則會得到四個橙色(#ff8000)背景。 您可以只創建上述xml可繪制對象的4個副本,然后將每個副本中的android:color
更改為唯一的陰影。
就是這樣;)
除了mjp66答案,我可以說使用支持庫中的GridLayout可以更好地完成此布局。 在這種情況下,如果有的話,您無需照顧多個列中的更改。
您的代碼將如下所示:
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:rowCount="2"
app:columnCount="2">
<View
android:id="@+id/top_left"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
app:layout_rowWeight="1"/>
<View
android:id="@+id/top_right"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
app:layout_rowWeight="1"/>
<View
android:id="@+id/bottom_left"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
app:layout_rowWeight="1"/>
<View
android:id="@+id/bottom_right"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
app:layout_rowWeight="1"/>
</android.support.v7.widget.GridLayout>
因此,您無需照顧寬度/高度,布局將自動執行。
<LinearLayout 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:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:background="#ff0000"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:background="#ff8000"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:background="#336699"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:background="#993366"/>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.