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

4

我想设置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的背景。

你有什么想法如何实现这个目标吗?


请查看:https://dev59.com/xV3Ua4cB1Zd3GeqP9yr3 - EvZ
不,这只会设置带有文本的行的背景。我想要整行(即使是空白处)都有相同的背景颜色。 - Time Travel
我认为你应该将11改为s.length(),这样你就可以得到字符串的总长度并设置它的背景。 - Reda
请查看https://dev59.com/bWAg5IYBdhLWcg3w_vOE#30096905。 - qw1nz
4个回答

0

将您的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一行的高度,并将bgcolor视图设置为该高度:

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);
    }
}

是的,这似乎是一个解决方案。但我需要设置多于1行的背景颜色。如果用户滚动行,背景颜色视图会如何移动? - Time Travel
哦,你想要突出显示特定的行? - dokkaebi
在这种情况下,我认为我会选择ListView,并根据其宽度将文本拆分成行。然后您可以免费获得行高亮显示。 - dokkaebi
好的,我会去探索一下。另外还有一个问题,如果我想让文本可编辑(可能可以使用TextView中的某些方法,在EditText中使用相同的方法并创建带有行高亮的可编辑文本框 - 有什么建议吗?) - Time Travel
这会让事情变得更加复杂;那么当用户编辑将一些文本推到下一行时,你必须考虑发生了什么。哪一行保留了高亮?此外,同样的情况也消除了ListView选项。在这种情况下,我认为我会坚持使用类似于我上面的答案,并且只需在文本更改时不断重新定位高亮视图。为此,请使用EditText#addTextChangedListener。我们谈论这个问题的越多,就越感觉可能会错过什么。这肯定以前做过... - dokkaebi
谢谢,这是相当丰富的信息,我可以开始着手实现我的目标。如果我有任何疑问,我可能会在这里或其他问题中发布。 :) - Time Travel

0
    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);

0
你的目标是使用类似于垂直方向的LineaLayout,其中包含一个TextView作为"可上色行"。
1)创建一个扩展LinearLayout的类。 2)编写基本函数以添加"可上色行","绘制它"等。 3)在布局XML中使用您的视图。

谢谢,虽然dokkaebi的答案似乎最合适,也是我需要实现的最简单的方法,但感谢您提供了一个创建自己视图的想法。这对我很有帮助。谢谢。 - Time Travel

-1

TextView(就像其他Android View一样)有方法setBackgroundColor

t.setBackgroundColor(0xffffffff);

但是这会设置整个TextView的背景颜色。我需要仅设置一行的背景颜色。 - Time Travel
哦...那就使用@dokkaebi的答案吧! - Budius
是的,dokkaebi的答案似乎是我可以用于我的要求的最合适的。我会探索这个方案。感谢您和@dokkaebi。 - Time Travel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接