android - 新手 : How to set attribute of the relative layout in my case?

标签 android android-layout android-emulator android-widget

我想像一样将我的屏幕分成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/

相关文章:

android - Webview 发布后不显示 url

android - 将原生 Google 帐户选择器与 B2C 结合使用

java - 为什么我的第一个应用程序没有出现在我的手机上?

android - recyclerview 中的 Seekbar 也会移动 recyclerview

Android Emulator 在 M1 mac/Apple Silicon 上卡住了加载屏幕

android - 如何更改Android模拟器临时目录

android - 捕获滑动以关闭事件

android - 插入从 "Internal test"轨道发布到生产

android - 应用图标仅在API21中显示,但在其他API中失败

android - 如何将多个触摸操作附加到单个列表项?