2016-08-29 3 views
0

У меня есть небольшой длинный файл макета, который я хочу реструктурировать, вытащив два куска из файла макета в независимые файлы макета и включив их в основной файл макета.Android: два разных макета включают в RelativeLayout

Моя проблема заключается в том, как сделать include2 подходить под include1 как в обычном режиме RelativeLayout. Значок include не знает layout_above и layout_below. Вместо этого решение будет использовать LinearLayout, но тогда кнопка, прикрепленная к нижней части экрана (в главном файле макета), больше не сможет удерживать свое место.

Основной файл макета: обновление

<FrameLayout 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="com.stats.weekly.WeeklyStatsFragment"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <include 
      android:id="@+id/include1" 
      layout="@layout/weekly_stats_header_view" /> 

     <!-- Here I can't tell include2 to position itself under include1 --> 
     <include 
      android:id="@+id/include2" 
      layout="@layout/weekly_stats_content_view" /> 

     <LinearLayout 
      android:id="@+id/statsButtonContainer" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true"> 

      <Button 
       android:id="@+id/statsButton" 
       style="@style/StandardButtonGreen" 
       android:layout_marginBottom="@dimen/material_horizontal_padding_half" 
       android:layout_marginLeft="@dimen/material_horizontal_padding_half" 
       android:layout_marginRight="@dimen/material_horizontal_padding_half" 
       android:layout_marginTop="@dimen/material_horizontal_padding_half" 
       android:elevation="2dp" 
       android:text="@string/strWeeklyStatsActivityButtonText" /> 

     </LinearLayout> 

    </RelativeLayout> 

</FrameLayout> 

Решение:

Как было предложено Чираг и Dipali, я добавил layout_width и layout_height так Layout_above и layout_below актуализируются:

... 
<include 
    android:id="@+id/include2" 
    layout="@layout/weekly_stats_content_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/weekly_stats_header_view" 
    android:layout_above="@+id/statsButtonContainer"/> 
... 

ответ

0

использовать подобные с относительным макетом, и если вы хотите использовать с линейным макетом используйте вес:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/include1" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_alignParentTop="true" 
     layout="@layout/weekly_stats_header_view" /> 

    <!-- Here I can't tell include2 to position itself under include1 --> 
    <include 
     android:id="@+id/include2" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_below="@+id/include1" 
     layout="@layout/weekly_stats_content_view" /> 

    <LinearLayout 
     android:id="@+id/statsButtonContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/include2"> 

     <Button 
      android:id="@+id/statsButton" 
      android:elevation="2dp" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="test" /> 

    </LinearLayout> 

</RelativeLayout> 
+0

Спасибо, Chirag, 'layout_below' не будет работать, не указывая также« layout_width »и« layout_height »в том же include. Сейчас работает отлично. – Ambran

+0

да именно он нуждается в них. –

0

Просто используйте LinearLayout с включенными функциями и кнопкой внутри RelativeLayout. Или 2 LinearLayouts - один с включает в себя, другая кнопке

0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relative_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:id="@+id/include1" 
     layout="@layout/demo1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <include 
     android:id="@+id/include2" 
     layout="@layout/demo2" 
     android:layout_below="@+id/include1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"/> 

    <LinearLayout 
     android:id="@+id/statsButtonContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="Button"/> 

    </LinearLayout> 

</RelativeLayout> 
+0

Что нужно: 'android: layout_below =" @ + id/relative_layout "'? – Shaishav

1

Используйте XML Просто добавьте layout_width и layout_height с layout_below Недвижимость в include2.

<include 
       android:id="@+id/include2" 
       layout_below = "@+id/include1" 
       layout_width = "match_parent" 
       layout_height = wrap_content" 
       layout="@layout/weekly_stats_content_view" />