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>
android:weightSum. Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.
By default, everything in a linear layout is Start and Top aligned. If you create a row of widgets by using a horizontal LinearLayout, the row will be push to the start/left side of the screen. If you create a set of widgets using a vertical LinearLayout, the column will be push to the top side of the screen.
If you want to divide layout into two parts of same size you have to set layout_width attributes to 0dp of both layouts and layout_weight to 1 inside the main layout. Then set weightSum to 2 of the parent layout. Same concept can be applied to divide layout to any ratio.
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"
.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With