如何在 Android XML 中将 Drawable 放置在 TextView 上
How to position Drawable over TextView in Android XML
我正在尝试在 XML 中创建删除线文本。我制作了一个可绘制对象(只是一条线),如果满足条件(使用数据绑定),则将其绘制在文本后面。问题是我的文字和删除线是不同的颜色,添加drawable的时候是画在文字后面的,看起来怪怪的。我需要它出现在文本之上,而不是在它后面。目前,我使用 background
Android 属性,所以它会在文本后面呈现是有道理的,但是我找不到任何其他选项。我在网上找到的唯一相关帮助是使用 drawableTop
,但是 drawable 根本不会出现。另外,我不能以编程方式执行此操作,我想在 xml 中执行此操作,这样我就不需要在其他地方引用我的观点。任何帮助,将不胜感激。这是我的一些代码:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/spacing_xs"
android:layout_marginLeft="@dimen/spacing_xs"
android:text="@{@string/text(viewModel.insertThisIntoString)}"
android:background="@{viewModel.displayStrikeThrough.value ? @drawable/strikethrough_text : null}"/>
和我的可绘制对象:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item>
<shape android:shape="line">
<stroke android:width="2dp"
android:color="@color/app_grey_300"/>
</shape>
</item>
</layer-list>
我在尝试通过 TextView 显示 ImageView 时使用的代码:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/spacing_xs"
android:layout_marginLeft="@dimen/spacing_xs"
android:text="@{@string/text(viewModel.insertThisIntoString)}"/>
<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/strikethrough_text"/>
</RelativeLayout>
理想情况下,我希望 ImageView 的 layout_width 跨越它所覆盖的 TextView 的宽度。我尝试的是将 RelativeLayout 的宽度设置为 wrap_content
(以便其宽度与 TextView 的宽度匹配),并将 ImageView 的宽度设置为 match_parent
,以便它填满RelativeLayout 的宽度,以及 TextView 的宽度,但是这不起作用 - 宽度将跨越整个屏幕。我目前将其设置为 40dp 以确保它会显示。
此外,应该注意的是,我尝试将可绘制对象同时设置为 ImageView 的背景和 src,但均无效。
由于您在 ImageView
中使用了 android:layout_height="wrap_content"
,因此它需要多少高度。但是你的 Drawable
不是位图,本身也没有大小。所以它的高度正好是 0px,因为它需要 0px 而没有尺寸。
您应该为高度设置一些具体值,或者使 ImageView
匹配 TextView
的大小,具体取决于您希望它的外观如何。
我正在尝试在 XML 中创建删除线文本。我制作了一个可绘制对象(只是一条线),如果满足条件(使用数据绑定),则将其绘制在文本后面。问题是我的文字和删除线是不同的颜色,添加drawable的时候是画在文字后面的,看起来怪怪的。我需要它出现在文本之上,而不是在它后面。目前,我使用 background
Android 属性,所以它会在文本后面呈现是有道理的,但是我找不到任何其他选项。我在网上找到的唯一相关帮助是使用 drawableTop
,但是 drawable 根本不会出现。另外,我不能以编程方式执行此操作,我想在 xml 中执行此操作,这样我就不需要在其他地方引用我的观点。任何帮助,将不胜感激。这是我的一些代码:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/spacing_xs"
android:layout_marginLeft="@dimen/spacing_xs"
android:text="@{@string/text(viewModel.insertThisIntoString)}"
android:background="@{viewModel.displayStrikeThrough.value ? @drawable/strikethrough_text : null}"/>
和我的可绘制对象:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item>
<shape android:shape="line">
<stroke android:width="2dp"
android:color="@color/app_grey_300"/>
</shape>
</item>
</layer-list>
我在尝试通过 TextView 显示 ImageView 时使用的代码:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/spacing_xs"
android:layout_marginLeft="@dimen/spacing_xs"
android:text="@{@string/text(viewModel.insertThisIntoString)}"/>
<ImageView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/strikethrough_text"/>
</RelativeLayout>
理想情况下,我希望 ImageView 的 layout_width 跨越它所覆盖的 TextView 的宽度。我尝试的是将 RelativeLayout 的宽度设置为 wrap_content
(以便其宽度与 TextView 的宽度匹配),并将 ImageView 的宽度设置为 match_parent
,以便它填满RelativeLayout 的宽度,以及 TextView 的宽度,但是这不起作用 - 宽度将跨越整个屏幕。我目前将其设置为 40dp 以确保它会显示。
此外,应该注意的是,我尝试将可绘制对象同时设置为 ImageView 的背景和 src,但均无效。
由于您在 ImageView
中使用了 android:layout_height="wrap_content"
,因此它需要多少高度。但是你的 Drawable
不是位图,本身也没有大小。所以它的高度正好是 0px,因为它需要 0px 而没有尺寸。
您应该为高度设置一些具体值,或者使 ImageView
匹配 TextView
的大小,具体取决于您希望它的外观如何。