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

时间:2019-12-20 18:41:13

标签: android textview imageview android-linearlayout android-relativelayout

我需要具有两个视图的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必须是一行。

1 个答案:

答案 0 :(得分:1)

下面的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"/>
相关问题