Android 在屏幕上平均定位四个相对布局

Android 在屏幕上平均定位四个相对布局,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我试图在屏幕上平均定位四个RelativeLayouts,如下图所示。我好像哪儿也去不了。每一个都包含一个文本标签和一个位于象限中心的按钮。我已尝试将这四个值放置在RelativeLayout中,并将alignComponent值设置为适当,将alignParent值设置为左/右和上/下。我也试过只使用alignComponent集合和alignParent集合,但没有效果 应该是这样的, 试试这个布局。。它可以帮助你 <?xml version="1.0" encoding="utf-8

我试图在屏幕上平均定位四个
RelativeLayouts
,如下图所示。我好像哪儿也去不了。每一个都包含一个文本标签和一个位于象限中心的按钮。我已尝试将这四个值放置在
RelativeLayout
中,并将
alignComponent
值设置为适当,将
alignParent
值设置为左/右和上/下。我也试过只使用
alignComponent
集合和
alignParent
集合,但没有效果

应该是这样的,


试试这个布局。。它可以帮助你

<?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="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/black" >

            <!-- put your stuff here -->
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/red" >

            <!-- put your stuff here -->
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/white" >

            <!-- put your stuff here -->

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/green" >

            <!-- put your stuff here -->

        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

示例输出

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#0B6121"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:background="#DF013A">
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" 
        android:background="#0080FF">
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#A901DB"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>



您可以使用LinearLayout或tableLayout轻松完成此操作, 使用LinearLayout的示例显示



因为您想使用
RelativeLayout
,最简单的方法是使用垂直和水平支柱(它们是不可见的),并将它们用作您的四个
RelativeLayout
孩子的锚定。这种方法肯定比带权重的嵌套
LinearLayout
s更有效。在嵌套带有权重的
LinearLayout
s之前,您可能应该先阅读

上面链接中的一个片段:

布局权重要求小部件测量两次。当具有非零权重的线性布局嵌套在另一个具有非零权重的线性布局中时,测量数量将以指数方式增加

因此,对于将
RelativeLayout
s与struts结合使用,您的XML可以如下所示:

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <View
        android:id="@+id/horizontalStrut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true" />

    <View
        android:id="@+id/verticalStrut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/horizontalStrut"
        android:layout_toLeftOf="@id/verticalStrut"
        android:background="@color/red" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/horizontalStrut"
        android:layout_toRightOf="@id/verticalStrut"
        android:background="@color/black" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/horizontalStrut"
        android:layout_toLeftOf="@id/verticalStrut"
        android:background="@color/blue" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/horizontalStrut"
        android:layout_toRightOf="@id/verticalStrut"
        android:background="@color/green" >
    </RelativeLayout>

</RelativeLayout>

您可以使用LinearLayout,然后为父布局设置android:weightSum,然后现在就可以为象限设置布局权重。如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum = "2"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/black" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/red" >
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/white" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/green" >
    </RelativeLayout>
</LinearLayout>


您应该为每个布局设置权重…您可以使用
LinearLayout
及其权重属性。。。为此,您可以使用LinearLayout或TableLayout轻松完成。使用默认位置上的第一个布局(左上方),第二个布局位于第一个的右侧,第三个布局位于第一个的下方,第四个布局位于第一个布局的下方,第三个布局位于第三个布局的右侧。@Manish Thnk You Buddy.:)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum = "2"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/black" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/red" >
    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/white" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/green" >
    </RelativeLayout>
</LinearLayout>