Android TextView:如何在TextView中设置整行的背景色

Android TextView:如何在TextView中设置整行的背景色,android,textview,background-color,Android,Textview,Background Color,我想在TextView中设置整行的背景色,而不一定是文本所覆盖的那部分(我可以使用Span) 例如,如果说第0行,可以容纳25个字符,但只有12个字符。如果我说 SpannableString s = new SpannableString("abcdefghijkl"); s.setSpan(new BackgroundColorSpan(0xffffffff), 0, 11, Spanned.SPAN_INCLUSIVE_INCLUSIVE); TextView t = new TextVi

我想在TextView中设置整行的背景色,而不一定是文本所覆盖的那部分(我可以使用Span)

例如,如果说第0行,可以容纳25个字符,但只有12个字符。如果我说

SpannableString s = new SpannableString("abcdefghijkl");
s.setSpan(new BackgroundColorSpan(0xffffffff), 0, 11, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
TextView t = new TextView();
t.setText(s);
这将设置一半线条的背景色。 但是我想用背景色填充整行(TextView的整个宽度)。另外,我希望这只发生在一行,而不是整个TextView背景

你知道如何做到这一点吗?

TextView(就像其他android视图一样)有这个方法


将TextView包装在RelativeLayout中,并在其后面放置另一个视图:

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
    <View android:id="@+id/bgcolor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/whatever"
        />
    <TextView android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</RelativeLayout>

您的目标是使用具有垂直方向的线性布局,
这将使TextView成为“可着色线”。

1) 创建扩展LinearLayout的类。
2) 编写基本函数以添加“可着色线条”、“绘制线条”和e.t.c.

3) 在layouts XML中使用视图。

检查:否这将仅设置包含文本的行部分的背景。我希望整行(甚至是空白)具有相同的背景色。我认为您应该将11更改为s.length(),您将获得字符串的总长度并设置其backgroundlook,但这将设置整个TextView的背景色。我只需要设置一行的背景色。哦。。。所以请使用@dokkaebi答案!是的,dokkaebi的答案似乎最适合我的需求。我会探索这一点。多亏了你和@dokkaebi。是的,这似乎是一个解决方案。但我需要设置超过1行的背景色。如果用户在这些行中滚动怎么办。背景色视图将如何移动?哦,您正试图突出显示某一行?在这种情况下,我想我将使用ListView,并根据其宽度将文本拆分为行。然后你可以免费获得线条突出显示。好的,我来探索一下。还有一个问题,如果我想使文本可编辑(可能如果TextView中有一些methid,我可以对EditText使用相同的methid,并创建带有高亮显示行的可编辑文本框-有什么建议吗?);然后,您必须考虑当用户编辑将某些文本推到下一行时会发生什么。哪一行保留高光?同样,同样的场景也会消除ListView选项。在这种情况下,我想我会坚持上面的答案,并在文本更改时继续重新定位突出显示视图。使用EditText#addTextChangedListener进行此操作。我们谈论得越多,就越觉得我们可能也错过了什么。这一定是以前做过的…谢谢,虽然dokkaebi的回答似乎最合适,对我来说最容易实现我的要求,但是谢谢你提供了一个关于如何创建我自己观点的想法。这将是有益的。谢谢
    TextView name = (TextView) findViewById(R.id.name);
    String iName = "YOYOYOYO";

    ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(184, 184,
            184));

    SpannableString ssb = new SpannableString(iName);
    ssb.setSpan(fcs, 0, iName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    name.setText(ssb);
final TextView textView = (TextView) findViewById(R.id.textview);
final ViewTreeObserver vto = textView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        int lineHeight = textView.getLineHeight();
        // May need to also getLineSpacingExtra, etc. not sure.
        View bgColor = findViewById(R.id.bgcolor);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) 
                bgColor.getLayoutParams();
        lp.height = lineHeight;
        bgColor.setLayoutParams(lp);
        // May or may not want to remove the listener:
        vto.removeGlobalOnLayoutListener(this);
    }
}
    TextView name = (TextView) findViewById(R.id.name);
    String iName = "YOYOYOYO";

    ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(184, 184,
            184));

    SpannableString ssb = new SpannableString(iName);
    ssb.setSpan(fcs, 0, iName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    name.setText(ssb);