简体   繁体   中英

Android: How to align an ImageView to the right of a TextView and make sure the ImageView is always visible

I need to have an Android layout with two views. The first view is a TextView while the second is an ImageView . The ImageView should always be aligned to the right of the TextView . The TextView should be able to expand to fill any remaining space depending on the size of the text. The ImageView should never be hidden by the TextView if the text is too big. The text should be tail truncated in this case.

Here's a visual of what I'm trying to accomplish:

在此处输入图片说明

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="bottom | center_vertical">
        <TextView
             android:id="@+id/myText"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:textColor="@android:color/black"
             android:ellipsize="end"
             android:singleLine="true"/>

        <ImageView
             android:id="@+id/myImage"
             android:layout_width="12dp"
             android:layout_height="12dp"
             android:src="@drawable/myImageDrawable"
             android:layout_toRightOf="@id/myText"/>
</RelativeLayout>

The above XML does not work because TextView hides the ImageView when its text is too big. How can I fix this code? I'm willing to use a different layout as well. Note that the TextView must be a single line.

The below xml is enough to achieve what you want. Just set singleLine to true and use drawableEnd to set your image. Also, replace the text in my code with yours. That's all.

    <TextView
        android:singleLine="true"
        android:text=" HehhsasasashasgghgahgshagshgahsghagshaghsgahsghaHehhsasasashasgghgahgshagshgahsghagshaghsgahsgha shgasagsasghag shahsghag"
        android:layout_width="wrap_content"
        android:drawableEnd="@drawable/myImageDrawable"
        android:layout_height="wrap_content"/>

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