W poniższym xml, próbuję narysować układ, który zawiera dwa bloki (LinearLayout i TextView). Chcę, aby LinearLayout był 5 razy większy niż TextView. Ten xml tworzy dokładne przeciwieństwo tego, czego się spodziewałem, TextView zajmuje 5 razy więcej miejsca niż LinearLayout. Zauważ, że ustawiłem szerokość obu elementów na 0 dp, co jest częstym niedopatrzymaniem.layout_weight działa w przeciwieństwie do tego, co powinno
<?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: Ten układ jest rzeczywiście element listy, które jest zawarte w tym liście:
<?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>
Zestaw: Android: layout_weight = "1" dla układu liniowego i Androida: layout_weight = "5" dla tekstu. to znaczy. wymień tam wagę. – Cropper
Tak, wiem, że to działa, ale pytanie brzmi raczej dlaczego? powinien mieć przeciwne zachowanie. – TrtG
To jest ... źle. Czy możesz dołączyć zrzut ekranu? Jeśli ostatnio zmieniłeś kolejność układów, spróbuj czystej kompilacji. – Geobits