簡體   English   中英

Android:如何將 ImageView 與 TextView 的右側對齊並確保 ImageView 始終可見

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

我需要一個帶有兩個視圖的 Android 布局。 第一個視圖是TextView而第二個是ImageView ImageView應始終與TextView的右側對齊。 TextView應該能夠根據文本的大小擴展以填充任何剩余空間。 如果文本太大, ImageView不應被TextView隱藏。 在這種情況下,文本應該被尾部截斷。

這是我要完成的工作的視覺效果:

在此處輸入圖片說明

<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>

上面的 XML 不起作用,因為TextView在其文本太大時隱藏了ImageView 我該如何修復此代碼? 我也願意使用不同的布局。 請注意, TextView必須是一行。

下面的 xml 足以實現你想要的。 只需將singleLine設置為true並使用drawableEnd來設置您的圖像。 另外,用你的代碼替換我的代碼中的文本。 就這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM