简体   繁体   English

为 TextView 的一部分设置可绘制背景

[英]Set drawable background for a portion of TextView

I have created a custom drawable for android:background attribute in a Textview.我为 Textview 中的android:background属性创建了一个自定义可绘制对象。 However I want the drawable background only for a certain part of the TextView and not the entire TextView但是我只想要 TextView 的某个部分而不是整个 TextView 的可绘制背景

Work done till now:到目前为止完成的工作:

在此处输入图像描述

Objective: I want to achieve something like this (ie set background for only substring(1,2) of text):目标:我想实现这样的目标(即仅为文本的substring(1,2)设置背景):

在此处输入图像描述

This is how I have achieved my current result:这就是我实现当前结果的方式:

overline.xml上划线.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="-2dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="1dp">
        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="#030310" />
        </shape>
    </item>
</layer-list>

layout.xml布局.xml

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/overline"
    android:textColor="#030310"
    android:text="Hello"/>

Here is what I have tried till now:这是我到目前为止所尝试的:

SpannableString ss = new SpannableString("Hello");

Drawable d = ContextCompat.getDrawable(((MainActivity) getActivity()).getApplicationContext(), R.drawable.overline);
d.setBounds(0, 0, 30, 30);

ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
ss.setSpan(span, 1, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

myTextView.setText(ss);

I do not understand ImageSpan quite well, and hence this is what I get after trying the above code (I am writing this code inside a Fragment loaded from MainActivity ),我不太了解 ImageSpan ,因此这是我在尝试上述代码后得到的(我在从MainActivity加载的Fragment中编写此代码),

在此处输入图像描述

Unfortunately, there is no out of the box Android solution for what you want to do as far as I know.不幸的是,据我所知,没有现成的 Android 解决方案可以满足您的需求。 You are on a good track looking at spans, but spans provide access to the underlying TextPaint and can only really do what TextPaint can do.您在查看跨度方面处于良好的轨道上,但跨度提供了对底层TextPaint的访问,并且只能真正完成TextPaint可以做的事情。 The exception to this is the ReplacementSpan which replaces the spanned text in its entirety.例外情况是ReplacementSpan ,它完全替换了跨区文本。 Your ImageSpan is a ReplacementSpan , so the rectangle replaces all the text and doesn't overlay as one may expect.您的ImageSpanReplacementSpan ,因此矩形替换了所有文本并且不会像预期的那样覆盖。

There are several ways to do what you ask, but the one that I prefer is outlined in Drawing a rounded corner background on text .有几种方法可以按照您的要求进行操作,但我更喜欢的方法在Draw a rounded corner background on text中进行了概述。 This would be a general solution but you would need to tweak it to make the rounded background your overline drawable.这将是一个通用解决方案,但您需要对其进行调整以使圆形背景成为您的上划线。

If that looks like overkill to you, and your text view is simple enough and you know that the line you want to draw will not cross line boundaries, you could also look at this .如果这对您来说看起来有点过头了,并且您的文本视图足够简单,并且您知道要绘制的线条不会越过线条边界,您也可以查看this

You could also create a simple View that is 2dp wide and as wide as you need to cover the letters with a background and move it programmatically.您还可以创建一个2dp宽的简单视图,并根据需要用背景覆盖字母并以编程方式移动它。 A custom View could also stretch over all of the text and you could draw the line in the view's onDraw() function.自定义视图也可以覆盖所有文本,您可以在视图的onDraw() function 中绘制线条。 A TextView StaticLayout can help determine where in the custom view the line should be drawn. TextView StaticLayout可以帮助确定应在自定义视图中的何处绘制线条。

I am sure that there are other solution available online if you search for them.如果您搜索它们,我相信还有其他可用的在线解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM