android - 在文本和 ImageView 之间放置空格

标签 android xml android-layout

我有以下 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>

您还可以在下图中看到问题。有文字和图片。图像为红色。

enter image description here

更新:

我使用 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 。

enter image description here

关于android - 在文本和 ImageView 之间放置空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37870058/

相关文章:

Android 应用屏幕尺寸不适合设备

Android Spinner 的外观和感觉都像 ICS

java - XML 文件出错 - Android(解析 XML 时出错 : not well-formed (invalid token))

xml - 如何在 jboss eap 6.2 应用程序运行时动态读取standalone.xml 文件?

c# - 如何从具有一些嵌入数据的 XML 文档填充 C# 类?

不同分辨率的 Android 布局

android - 如何知道当我启动 Android 设备时哪些应用程序将自动启动?

java - 任何解决 onTouchEvent 上奇怪的验证程序拒绝类的想法

xml - XML 元素应该有分组父元素吗?

java - 缩放后getWidth不返回不同的值