我有以下 xml。我有 textview 和 imageview。我想在这些项目之间留出一点空间(比如 20dp),但根据 TextView 的长度,它有时会重叠。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:id="@+id/myName"
android:textSize="14sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/myName"
android:gravity="center_vertical|right"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:src="@drawable/ic_arrow_drop_down_red"
android:paddingLeft="20dp" />
</RelativeLayout>
您还可以在下图中看到问题。有文字和图片。图像为红色。
更新:
我使用 android:drawableRight
遵循了以下方法,但得到了同样的结果。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:textColor="@color/primary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:id="@+id/myName"
android:textSize="14sp"
android:drawableRight="@drawable/ic_arrow_drop_down_red"
android:drawablePadding="20dp" />
</RelativeLayout>
最佳答案
如果您只想在 TextView
的右侧(或任何其他侧)显示可绘制对象,请考虑使用 android:drawableRight
属性而不是两个 View .
图像可以通过android:drawablePadding
进一步分离。如果您的 UI 更复杂,这可能还不够,您将不得不处理两个 View 。
关于android - 在文本和 ImageView 之间放置空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870058/