Search code examples
androidandroid-linearlayoutandroid-layout-weight

Why is layout weight not working if I use .75 and .25?


This is my XML:

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

    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="8 diciembre, 2015"
        android:id="@+id/txtFecha"
        android:textAlignment="textEnd"
        android:layout_margin="5dp"
        android:layout_gravity="right" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:layout_margin="10dp">

        <TextView
            android:layout_width="0dp"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text"
            android:id="@+id/txtTituloNot"
            android:layout_height="wrap_content"
            android:maxHeight="52dp"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:src="@drawable/feclaabajo"
            android:layout_below="@+id/txtTituloNot"
            android:scaleType="fitXY"
            android:adjustViewBounds="false"
            android:layout_weight="1" />

    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text Medium Text Medium Text Medium Text
        Medium Text Medium Text Medium Text Medium Text Medium Text
        Medium Text Medium Text Medium Text Medium Text Medium Text
        Medium Text Medium Text Medium Text Medium Text Medium Text Medium"
        android:id="@+id/txtExtracto"
        android:layout_margin="5dp"
        android:maxHeight="110dp"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="233dp"
        android:layout_height="170dp"
        android:id="@+id/imageViewNoticia"
        android:src="@drawable/imagenodisponible"
        android:soundEffectsEnabled="false"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitXY"
        android:layout_marginBottom="7dp" />

</LinearLayout>

But now I am working with below code only:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1"
    android:layout_margin="10dp">

    <TextView
        android:layout_width="0dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text Large Text"
        android:id="@+id/txtTituloNot"
        android:layout_height="wrap_content"
        android:maxHeight="52dp"
        android:layout_weight="1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/feclaabajo"
        android:layout_below="@+id/txtTituloNot"
        android:scaleType="fitXY"
        android:adjustViewBounds="false"
        android:layout_weight="1" />

</LinearLayout>

As you seen the imageview and textview have:

android:layout_weight="1"

But this see as it:

The image view not sees, and Textview has the 100%

Any value I set to Imageview this does not change

Now if I change the value of text but a value between 0 and .99 (image view yet with 1 in weight):

This work fine if I use .75 and .25 but it does not work if I set 3 and 1 I would like to be able of use the last


Solution

  • You have defined a weightSum of 1 for the parent LinearLayout.

    By doing so, you are telling the LinearLayout that the maximum weight of the sum of the children weights will be 1, but then you are setting both children to have a weight of 1, which adds up to 2.

    The simplest solution is to simply remove the weightSum attribute, since it isn't necessary in this case and then you won't have to remember to keep it in sync.