2014-11-07 3 views
1

Я пытаюсь применить вес на андроиде LinearLayout состоит из 3 элементов для TextView, чтобы занять самое доступное пространство. Ориентация макета вертикальная, и я применяю weight="1" на TextView и не набираю вес на двух других элементах, чтобы заставить их располагаться в нижней части экрана. Однако это не работает, так как «большой» TextView не занимает места вообще, а второй элемент LinearLayout, который является Relativelayout, занимает все пространство. Вот раскладка:Вес в LineraLayout с RelativeLayout ребенок не работает

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:weightSum="1" > 

     <TextView 
      android:id="@+id/txt_message" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:isScrollContainer="true" 
      android:maxLines="100000000" > 

     </TextView> 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/answer_panel" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@color/my_color"/> 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="5dp" 
      android:text="@string/answer_lbl"> 

     </TextView> 

     <TextView 
       android:id="@+id/txt_answer" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:isScrollContainer="true" 
       android:lines="6" 
       android:maxLines="100000000"> 
     </TextView> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@color/my_color"/> 

     </LinearLayout> 


     <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:layout_marginBottom="5dp" 
       android:layout_alignParentBottom="true" 
       android:gravity="center"> 

     <Button android:id="@+id/my_button" 
       android:layout_height="wrap_content" 
       android:focusable="false" 
       android:layout_width="wrap_content" 
       android:background="@color/my_button" 
       android:textSize="14sp" 
       android:textColor="@color/background_color" 
       android:text="@string/txt_my_button"/> 


    </LinearLayout> 

    </RelativeLayout> 

    </LinearLayout> 

Я пытался также embeed в txt_message TextView в Linearlayout, но это ничего не меняет. Как я могу поместить элементы в Relativelayout в нижней части экрана и позволить первым TextView занимать оставшееся пространство?

+0

Я вижу только два элемента в корневой LinearLayout, в TextView и RelativeLayout, это правильно? –

+0

Я вижу некоторые аномалии в этом макете. ** 1 ** - 'fill_parent' устарел с уровня API 8 (несколько лет назад) и заменен на' match_parent'. ** 2 ** - Я понимаю, что у вас нет четкого представления о том, как работают весы: должно быть больше, чем только один взвешенный вид в LinearLayout, или он полностью неэффективен. И weightSum полностью ** опционально **. –

+0

Если высота LinearLayout соответствует match_parent и weightsum = 1, ваш первый элемент с весом = 1 должен занимать всю высоту linearlayout, т. Е. Всю высоту макета. –

ответ

0

сделать вас сверху Линейная высоту расположения андроида: layout_height = «fill_parent» или («match_parent») теперь обернуть содержание .. и удалить относительное расположение - он не делает любую работу для вас

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txt_message" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#ffff00" 
     android:isScrollContainer="true" 
     android:maxLines="100000000" > 
    </TextView> 



     <LinearLayout 
      android:id="@+id/answer_panel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <View 
       android:layout_width="fill_parent" 
       android:layout_height="1dp" 
       android:background="#ff0000" /> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="5dp" 
       android:layout_marginTop="5dp" 
       android:text="sddsds" > 
      </TextView> 

      <TextView 
       android:id="@+id/txt_answer" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:isScrollContainer="true" 
       android:lines="6" 
       android:maxLines="100000000" > 
      </TextView> 

      <View 
       android:layout_width="fill_parent" 
       android:layout_height="1dp" 
       android:background="#ff0000" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="5dp" 
      android:layout_marginTop="10dp" 
      android:gravity="center" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/my_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#ff0000" 
       android:focusable="false" 
       android:text="dfgfdgfdg" 
       android:textColor="#ff00ff" 
       android:textSize="14sp" /> 
     </LinearLayout> 


</LinearLayout> 
+0

определения цвета заменяются жесткими ячейками ... верните его обратно к используемому вами – Aun

+0

Я скорректировал макет вида в соответствии с вашими предложениями. После удаления Relativelayout проблема исчезла. Я заметил это отклоняющееся поведение на еще одном макете, хотя и не нашел инструкции не использовать Relativelayout в качестве дочернего элемента при использовании весов. – arjacsoh

0

Если вы хотите, чтобы текстовое изображение имело больше веса, чем другие элементы внутри linearlayout, дайте текстуру весом 5, например, а остальные элементы - весом 1. Играйте с весами ...

Другой вариант может быть из LinearLayout, RelativeLayout с фиксированной высотой ... Если ваша родительская группа просмотра RelativeLa yout, поместите RelativeLayout с помощью android: layout_alignParentBottom = "true". Если это LinearLayout, вы должны пойти с первым вариантом.

После прочтения вашего комментария вы можете использовать:

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

    <TextView 
     android:id="@+id/txt_message" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:isScrollContainer="true" 
     android:maxLines="100000000" 
     android:above="@+id/relativeLayoutContainer" > 

    </TextView> 

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

    <LinearLayout 
     android:id="@+id/answer_panel" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="@color/my_color"/> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:text="@string/answer_lbl"> 

    </TextView> 

    <TextView 
      android:id="@+id/txt_answer" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:isScrollContainer="true" 
      android:lines="6" 
      android:maxLines="100000000"> 
    </TextView> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="@color/my_color"/> 

    </LinearLayout> 


    <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:layout_marginBottom="5dp" 
      android:layout_alignParentBottom="true" 
      android:gravity="center"> 

    <Button android:id="@+id/my_button" 
      android:layout_height="wrap_content" 
      android:focusable="false" 
      android:layout_width="wrap_content" 
      android:background="@color/my_button" 
      android:textSize="14sp" 
      android:textColor="@color/background_color" 
      android:text="@string/txt_my_button"/> 


</LinearLayout> 

</RelativeLayout> 

</RelativeLayout> 
+0

Я хочу, чтобы у него было самое доступное. Второй элемент имеет фиксированный размер, а первый удар - остальные. – arjacsoh