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 の背景全体ではなく、1 行だけで発生させたいと考えています。

これをどのように達成できるか考えていますか?

4

4 に答える 4

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);
于 2013-01-11T18:53:46.323 に答える
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);
    }
}
于 2013-01-11T16:33:46.730 に答える
0


あなたの目標は、「色付け可能な線」として TextView を持つLineaLayout のようなものを垂直方向に使用することです。

1) LinearLayout を拡張するクラスを作成します。
2)「色付け可能な線」、「ペイント」などを追加する基本的な関数を
記述します 3)レイアウト XML でビューを使用します。

于 2013-01-11T16:44:19.283 に答える
-1

TextView (他のすべての Android ビューと同様) にはメソッドsetBackgroundColorがあります

t.setBackgroundColor(0xffffffff);
于 2013-01-11T16:21:26.307 に答える