简体   繁体   中英

Android TextView width with ImageView

I have a ListView cell with a TextView and an ImageView. I would like the ImageView to stay on the right side and the TextView width to be "the cell width minus all space taken up by the ImageView". Right now I am using this:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/defaultCellHeight">

        <TextView
            android:id="@+id/randomSettingShow"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/defaultCellHeight"
            android:paddingLeft="30dp"
            android:textSize="@dimen/defaultTextSize"
            android:focusable="false"
            android:textStyle="normal"
            android:gravity="center_vertical|left"
            android:layout_alignParentLeft="true" />

         <ImageView
            android:id="@+id/randomSettingShowCheck"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/defaultCellHeight"
            android:gravity="center"
            android:layout_marginRight="15dp"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/checkmark" />
</RelativeLayout>

But the problem is that if the text in the TextView is long enough, the text goes under the ImageView and obviously, this is incorrect. I would like the TextView to have a max width set to the left border of the ImageView.

How can I do that?

您可以尝试将TextView与可绘制的复合集一起使用- 请参阅文档

You can try something like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_toLeftOf="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />
</RelativeLayout>

Like Asahi said, a compound drawable will probably be better suited for your case. Take a look at this answer here .

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