Reputation: 16584
this is currently my row layout for a listview:
<?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:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:text="99.99.9999"
android:layout_weight="0.2"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="This is a correct looking text"
android:id="@+id/txtComment"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:layout_marginRight="5dp"
android:text="-1234,56" />
</LinearLayout>
</LinearLayout>
The problem is within the TextView with id txtComment
. Everything is looking nicely for short text content and text content that contains regular line breaks.
But as soon as there is a long text without line breaks, Android still wraps the text but also changes the weight of the surrounding LinearLayout so that the left and right textviews are being compressed.
How can I make sure that for long texts without line breaks Android still respects the weights of the elements?
Screenshots showing the issue:
Upvotes: 1
Views: 2787
Reputation: 9569
There is much better and cleaner way to achieve that:
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="4dp"
app:alignmentMode="alignBounds"
app:columnCount="3"
app:rowOrderPreserved="false"
app:useDefaultMargins="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="99.99.9999"
app:layout_gravity="fill_horizontal" />
<TextView
android:id="@+id/txtComment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text"
app:layout_columnWeight="3"
app:layout_gravity="fill_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="right"
android:text="-1234,56"
android:textStyle="bold"
app:layout_gravity="fill_horizontal" />
</android.support.v7.widget.GridLayout>
Also don't forgot to add in your gradle file:
compile 'com.android.support:gridlayout-v7:24.1.0'
Upvotes: 0
Reputation: 1694
In your root element: android:weightSum="1"
In your child elements android:layout_width="0dp"
Upvotes: 0
Reputation: 8149
you just have to set your android:layout_width="wrap_content"
to android:layout_width="0dip"
where you set weights.
<?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:orientation="horizontal">
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_weight="0.2"
android:text="99.99.9999" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="vertical">
<TextView
android:id="@+id/txtComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="This is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking textThis is a correct looking text" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="right"
android:text="-1234,56"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
Upvotes: 1
Reputation: 7082
The thing is, the layout works alright :-) You set the weights as 20%, 60% and 20%, which is exactly what happens in the last case.
Also, you should set android:layout_width="0dp"
when using LineraLayout
weights.
Upvotes: 0
Reputation: 6828
Try this,
The weights are not working properly because you have set the width as wrap_content
. Change it to 0dp
.
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:text="99.99.9999"
android:layout_weight="0.2"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="This is a correct looking text"
android:id="@+id/txtComment"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:layout_marginRight="5dp"
android:text="-1234,56" />
</LinearLayout>
Upvotes: 3