我想像田一样将我的屏幕分成4个相等的区域。四个区域中的每一个都是线性布局。
我尝试使用相对布局来容纳四个线性布局,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/up_left_area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff66"
>
<TextView
android:id="@+id/label1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="UP LEFT"/>
</LinearLayout>
<LinearLayout
android:id="@+id/up_right_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/up_left_area"
android:background="#ccffff">
<TextView
android:id="@+id/label2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="UP RIGHT"/>
</LinearLayout>
<LinearLayout
android:id="@+id/down_left_area"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@id/up_left_area"
android:background="#66cc33"
>
<TextView
android:id="@+id/label3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="DOWN LEFT"/>
</LinearLayout>
<LinearLayout
android:id="@+id/down_right_area"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/up_right_area"
android:layout_toRightOf="@id/down_left_area"
android:background="#cc6600">
<TextView
android:id="@+id/label4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="DOWN RIGHT"/>
</LinearLayout>
</RelativeLayout>
使用上面的 xml 布局代码,我在屏幕上得到了 4 个区域,但它们不大小相等。如何修改我的代码,使屏幕上有大小相等的 4 个区域,如 田 ?
最佳答案
您可以使用具有相同 layout_weight=1 的线性布局。
简化的 xml:
- linearlayout orientation=vertical, layout_width=fill_parent, layout_height=fill_parent
-- linearLayout orientation=horizontal, layout_weight=1, layout_width=fill_parent, layout_height=fill_parent
-- -- linearLayout layout_weight=1, layout_width=fill_parent, layout_height=fill_parent
-- -- -- .... LEFT TOP
-- -- linearLayout layout_weight=1, layout_width=fill_parent, layout_height=fill_parent
-- -- -- .... RIGHT TOP
-- linearLayout orientation=horizontal, layout_weight=1, layout_width=fill_parent, layout_height=fill_parent
-- -- linearLayout layout_weight=1, layout_width=fill_parent, layout_height=fill_parent
-- -- -- .... LEFT BOTTOM
-- -- linearLayout layout_weight=1, layout_width=fill_parent, layout_height=fill_parent
-- -- -- .... RIGHT BOTTOM
...类似的东西。
关于android - 新手 : How to set attribute of the relative layout in my case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6531424/