2013-04-13 6 views
0

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

У меня может быть больше компонентов в каждом внутреннем LinearLayout, например, трех левых и трех правых кнопках. Ниже приведен упрощенный пример моего кода

пример ниже: main_layout.xml

<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:paddingTop="5dp" 
    android:baselineAligned="false" 
    android:orientation="vertical" > 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.95" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_weight="0.8" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" > 

      <include layout="@layout/child_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_weight="0.2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" > 

      <include layout="@layout/child_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
     </TableRow> 
    </TableLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_weight="0.05" 
     android:gravity="center" 
     android:orientation="vertical"> 

     <Button 
      android:layout_width="80dp" 
      android:layout_height="40dp" 
      android:textSize="14sp" 
      android:textStyle="bold" 
      android:text="Bottom" /> 
    </LinearLayout> 

</LinearLayout> 

child_layout

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:id="@+id/leftLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="80dp" 
      android:layout_height="40dp" 
      android:textSize="14sp" 
      android:textStyle="bold" 
      android:text="Left" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/rightLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="80dp" 
      android:layout_height="40dp" 
      android:textSize="14sp" 
      android:textStyle="bold" 
      android:text="Right" /> 
    </LinearLayout> 

</RelativeLayout> 

Обратите внимание, что левая и правая кнопки перекрываются на изображении. enter image description here. Как развернуть включенный макет, чтобы заполнить всю ширину родителя, чтобы левая и правая кнопки отображались слева и справа. Спасибо.

ответ

0

В вашем childlayout, достаньте LinearLayouts затем присвоить их слева и справа от родительского

<Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Left" 
     android:layout_alignParntLeft="true" /> 


    <Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Right" 
     android:layout_alignParntRight="true"/> 

Если я понимаю, что вы хотите, то это должно работать. Это позволит получить их на левый и правый, но в зависимости от того, если вы хотите, чтобы они на той же «линии» выше или ниже друг друга, то есть и другие атрибуты, которые вы можете установить

RelativeLayout

LinearLayout может быть очень легко и полезно, когда это необходимо, но с использованием RelativeLayout часто раз устраняют вложенными LinearLayout с и бесполезный ViewGroup s

+0

У меня может быть больше компонентов в каждом LinearLayout, например, три левых кнопки и три правой кнопки. Я предоставил упрощенный пример моего кода. Должен ли я назначать слева и справа все внутренние компоненты? – sony

+0

Да, где бы вы ни были. Похоже, что больше работы и труднее понять, но IMHO, «RelativeLayout» - это простое управление, когда вы это понимаете. И это намного более гибко – codeMagic

+0

Я добавил примечание к моему ответу на мои мысли – codeMagic

0

Вы можете также избавиться от нескольких LinearLayouts и RelativeLayout и сделать это вместо того, чтобы:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Left" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="vertical" /> 

    <Button 
     android:layout_width="80dp" 
     android:layout_height="40dp" 
     android:textSize="14sp" 
     android:textStyle="bold" 
     android:text="Right" /> 
</LinearLayout> 
Смежные вопросы