2016-11-23 9 views
-2

У меня есть два линейных выхода, каждый из которых занимает 50% от общей площади. Внутри них есть разные виды, и я не понимаю, почему horizontalScrollView1 и listView1 (посмотрите идентификаторы) не отображаются. Два вида должны расширяться, чтобы заполнить пространство, оставшееся в родительском. Что я делаю неправильно?Почему android layout_weight не работает должным образом?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout   
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent"> 
     <HorizontalScrollView 
     android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
     <HorizontalScrollView 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:id="@+id/horizontalScrollView1"> 
     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/list"/> 
     </HorizontalScrollView> 
    </LinearLayout> 
    <LinearLayout   
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent" > 
     <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello!!!" /> 
     <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:id="@+id/listView1"/> 
    </LinearLayout> 
</LinearLayout> 
+0

Вы забыли добавить тип ориентации [android: orientation = "vertical"] для обоих родительских линейных макетов, имеющих весовую сумму, для проверки уточнения ответа на готовность. –

ответ

3

Вы забыли установить android:orientation="vertical" как к LinearLayout основного LinearLayout. Таким образом, ребенок LinearLayout расположен в вертикальном направлении и будет виден.

0

Вы используете match_parent для своих элементов, поэтому, когда один из них принимает всю ширину, у другого нет места для рисования. Установите ширину внутренних элементов на wrap_content. И ориентации на LinearLayout, если вы не делаете этого, вес не работает.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout   
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:orientation="vertical"> 
     <HorizontalScrollView 
     android:id="@+id/header" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
     <HorizontalScrollView 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="wrap_content" 
     android:id="@+id/horizontalScrollView1"> 
     <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/list"/> 
     </HorizontalScrollView> 
    </LinearLayout> 
    <LinearLayout   
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:orientation="vertical"> 
     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello!!!" /> 
     <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:id="@+id/listView1"/> 
    </LinearLayout> 
</LinearLayout> 
0

Проверьте это, я покрасил необходимые блоки и разделил их.

<?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="match_parent" 
     android:orientation="vertical"> 
     <LinearLayout 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:background="@color/accent" 
      android:layout_width="match_parent"> 
      <HorizontalScrollView 
       android:id="@+id/header" 
       android:background="@color/primary" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 
      <HorizontalScrollView 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:background="@color/black" 
       android:id="@+id/horizontalScrollView1"> 
       <ListView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:id="@+id/list"/> 
      </HorizontalScrollView> 
     </LinearLayout> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:background="@color/green" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:layout_width="match_parent" > 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Hello!!!" /> 
      <ListView 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" 
       android:id="@+id/listView1"/> 
     </LinearLayout> 
    </LinearLayout> 

В основном вы забыли добавить ориентацию = вертикальный в некоторых местах, а также необходимо указать андроида: layout_weight = «1» для обоих HorizontalScrollView.

0

Это происходит потому, что вы не указали вес для горизонтального прокрутки @ + id/header. Таким образом, он занимает полную высоту его родительской линейной компоновки. И вы забыли добавить тип ориентации для обоих родительских линейных макетов.

Вы можете проверить этот макет xml.

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

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

     <HorizontalScrollView 
      android:id="@+id/header" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 

     <HorizontalScrollView 
      android:id="@+id/horizontalScrollView1" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 

      <ListView 
       android:id="@+id/list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </HorizontalScrollView> 
    </LinearLayout> 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Hello!!!" /> 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 
Смежные вопросы