2013-12-16 4 views
0

Как установить несколько макетов на одном и том же представлении, чтобы они оба разделили пространство одинаково?Установить несколько видов/макетов в родительском макете одинаково

Следующая URL имеет изображение для моего требования:

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

Каждый из RelativeLayouts должны быть установлены один над другим. Пожалуйста предложить modificiation необходимое для моего текущего 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> 

ответ

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> 

Обратите внимание, что андроид: layout_width =» 0dp "или android: layout_height =" 0dp "используется для оптимизации ширины/высоты.

+0

Эй, спасибо за answer.Further, если нам нужно поместить горизонтальный разделитель в 2/3 говорят экрана (в этом случае каждый макет занимает 2/3, 1/3 соответственно), что должно быть необходимой модификацией? –

+0

@LauraStone вы можете использовать Linear Layout в качестве родительского макета и два линейных макета в качестве дочерних элементов и придать весу согласно вашему требованию. – Piyush

+0

@LauraStone вы должны использовать атрибут веса. В вашем примере вам нужно установить layout_weight = «2» для первого представления, а «1» - для второго. Общее количество будет 3, первое занятие 2/3, а secod 1/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> 
Смежные вопросы