简体   繁体   中英

How to set the width of a layout in android that extends to a point relative to the right border?

I have a listview that has a small and a long textview (with varying texts in both) on the left and an imageview on the far right. Problem is that when the long textview gets too long it compresses the imageview on the right before wrapping into a 2nd line. Since both textviews have various lengths in each line, I cannot set a fix dp value for their length. What can I do that the imageview does not get compressed ?

PS: I create the rows in the listview like this:

<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:layout_gravity="center_vertical"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_gravity="center_vertical"/>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ImageView
android:id="@+id/image"
android:layout_width="45dp" 
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:focusable="false"/>

</RelativeLayout>

Add android:toLeftOf="@+id/image" to your TextViews. You may need to set the layout_width of the TextViews to fill_parent.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:toLeftOf="@+id/image"
android:layout_gravity="center_vertical"/>

<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:toLeftOf="@+id/image"
android:layout_gravity="center_vertical"/>

<ImageView
android:id="@+id/image"
android:layout_width="45dp" 
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:focusable="false"/>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM