【问题标题】:Put space between text and imageview在文本和图像视图之间放置空间
【发布时间】:2016-06-16 22:00:36
【问题描述】:

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

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    如果您只想在TextView 的右侧(或任何其他侧)显示可绘制对象,请考虑使用android:drawableRight 属性而不是两个视图。

    图片可以用android:drawablePadding进一步分隔。如果您的 UI 更复杂,那可能还不够,您将不得不处理两个视图。

    【讨论】:

    • 这也是一个很好的方法。
    【解决方案2】:

    您可以像这样在这两者之间插入一个具有所需宽度的空视图(也可以将布局更改为LinearLayout)。

    编辑 更改为线性布局后,空视图解决方案和drawableRight 解决方案都有效,所以我正在编辑我的解决方案,使其看起来更漂亮。 drawableRight 的所有功劳归于@JuanCortes:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayoutxmlns: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">
    <TextView
        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:paddingLeft="20dp" />
    
    </LinearLayout>
    

    【讨论】:

    • 不幸的是,我仍然得到相同的结果。即使我删除了应用程序并清理并重建了它。
    • 嗯,摆脱相对布局,将其置于线性布局中。还要去掉toLeftOf 和类似的,因为它们是相对布局所独有的。相对布局让我毛骨悚然。
    • 是的,即使使用drawableRight 也可以工作,无需额外的视图或图像视图。请更新您的答案,然后我会将其标记为答案。
    • 好的,很酷。谢谢。编辑。你需要开始学习使用线性布局,它们是我来征服并发现自己最舒服的工作,并保持对它们的控制。您可以随意嵌套它们,并使用权重来确定它们的相对大小。
    【解决方案3】:

    尝试使用Framelayout 可能对你有帮助

    <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">
        <FrameLayout
            android:layout_width="match_parent"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_height="wrap_content">
            <TextView
    
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_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:layout_gravity="center_vertical|right"
                android:layout_centerVertical="true"
                android:layout_alignParentRight="true"
                android:paddingLeft="20dp" />
        </FrameLayout>
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多