Android:如何将ImageView对齐到TextView的右边,并确保ImageView始终可见

问题描述 投票:0回答:1

我需要具有两个视图的Android布局。第一个视图是TextView,第二个视图是ImageViewImageView应始终与TextView的右侧对齐。 TextView应该能够扩展以填充任何剩余的空间,具体取决于文本的大小。如果文本太大,则ImageView绝不能被TextView隐藏。在这种情况下,文本应被截尾。

这是我要完成的工作的视觉效果:

enter image description here

<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必须为一行。

android textview imageview android-linearlayout android-relativelayout
1个回答
0
投票

下面的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"/>
© www.soinside.com 2019 - 2024. All rights reserved.