Pregunta

I am creating a design in Android for this list item:

enter image description here

I thought that the best solution would be create a linear layout with two relative layout inside it and a space. So I have tried to do it but I have a problem with weights. In this picture you can see that I have added two relative layout with a space using layout_weight of linear_layout and the proportion works.

enter image description here

This is the code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/fondo_chiste_lista_item"
    android:weightSum="147"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_weight="41"
        android:layout_height="0dp" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_weight="94"
        android:layout_height="0dp" >
    </RelativeLayout>

    <android.support.v7.widget.Space
        android:id="@+id/space1"
        android:layout_width="wrap_content"
        android:layout_weight="12"
        android:layout_height="0dp" />

</LinearLayout>

But, now If I add a textview inside a relative layout proportions won't work. As you can see the boxes of the relative layouts have a different size.

enter image description here

This is the code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/fondo_chiste_lista_item"
    android:weightSum="147"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_weight="41"
        android:layout_height="0dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="46dp"
            android:text="TextView" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_weight="94"
        android:layout_height="0dp" >
    </RelativeLayout>

    <android.support.v7.widget.Space
        android:id="@+id/space1"
        android:layout_width="wrap_content"
        android:layout_weight="12"
        android:layout_height="0dp" />

</LinearLayout>

So... What is the problem? Why the proportion change when I add elements? Is there any better solution?

¿Fue útil?

Solución

I will add is as an answer since that fixed your problem

instead of

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

use something like

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top