2013-12-16 126 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>