同样在父布局中设置多个视图/布局

时间:2013-12-16 13:37:47

标签: android xml android-layout

如何在同一视图上设置多个布局,以便它们共享空间?

以下网址包含符合我要求的图片:

How to set two different layouts for one view using constraints?

每个RelativeLayouts都要一个放在另一个上面。 请为此建议我当前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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ScrollAct" >

    <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

<RelativeLayout
          android:id="@+id/rlExtendBar;"
                android:layout_width="match_parent"
    android:layout_height="?"> 
    </RelativeLayout>
    <RelativeLayout
          android:id="@+id/rel_layout"
                android:layout_width="match_parent"
    android:layout_height="?">

    </RelativeLayout>
 </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

在我看来,你需要将两个孩子放在一个父母中。您需要使用属性

  

机器人:layout_weight = “1”

做你想做的事。

所以,如果你想要垂直划分屏幕,你必须做类似的事情:

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

<RelativeLayout 
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:layout_weight="1" >

    <!-- Here, the content-->

</RelativeLayout>

<RelativeLayout 
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:layout_weight="1" >

    <!-- Here, the content-->

</RelativeLayout>
</LinearLayout>

横向划分:

<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout 
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1" >

    <!-- Here, the content-->

</RelativeLayout>

<RelativeLayout 
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="1" >

    <!-- Here, the content-->

</RelativeLayout>
</LinearLayout>

请注意,android:layout_width =“0dp”或android:layout_height =“0dp”用于优化宽度/高度。

答案 1 :(得分:0)

尝试使用此

<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="vertical" >

    <LinearLayout
        android:background="#FF9300"
        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" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
               android:text="Text" />
       </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:background="#FFFB00"
        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" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />
        </LinearLayout>

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Text" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>