2013-12-16 82 views
0

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

以下网址有我的要求图像:

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

每个都铺设了另一种。 请建议需要我当前XML这种情况的modificiation:

<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> 

回答

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“用于优化宽度/高度。

+0

嘿,感谢您的answer.Further,如果我们需要把水平分隔在说屏幕的2/3(在这种情况下,每个布局花费2/3,1/3空间),应该做什么修改? –

+0

@LauraStone您可以将线性布局作为父布局,将两个线性布局作为孩子,并根据您的要求给予权重。 – Piyush

+0

@LauraStone你必须使用weight属性。在你的例子中,你必须为第一个视图设置layout_weight =“2”,为第二个视图设置“1”。总数将是3,第一次占2/3,第三次占三分之一。 – FR073N

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>