Вопрос

In the following xml, I'm trying to draw a layout that contains two blocks (a LinearLayout and a TextView). I want the LinearLayout to be 5 times larger than the TextView. This xml produces the exact opposite of what I expected, the TextView takes 5 times more space than the LinearLayout. Notice that I have set the width of both elements to 0dp which is a common oversight.

<?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:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:layout_gravity="center_vertical"
    android:weightSum="6"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="5" >

        <TextView
            android:id="@+id/result_title_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#222222"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/result_info_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="Arial"
            android:textColor="#777777"
            android:textSize="12sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/result_distance_textview"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fontFamily="Arial"
        android:textColor="#ffffffff"
        android:textSize="14sp" />

</LinearLayout>

EDIT: This Layout is actually a list item which is included in this list :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/results_list_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="15dp" >

    <ListView
        android:id="@+id/search_results_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>
Это было полезно?

Решение 2

The issue came from the container list which had a "wrap_content" width. Changing to "match_parent" fixed the problem.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/results_list_fragment_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingTop="15dp" >

    <ListView
        android:id="@+id/search_results_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/scrollable_content_background"
        android:divider="@drawable/listview_divider"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>

Другие советы

I also just met the same problem, and the way of solving is to change:

  • width of child view to 0dp if parent LinearLayout has orientation="horizontal";

  • height of child view to 0dp if parent LinearLayout has orientation="vertical".

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top