1

私はこのような形状属性を使用しています:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:padding="10dp">
<solid
    android:color="#FFFFFF" />
<corners
    android:bottomRightRadius="15dp"
    android:bottomLeftRadius="15dp"
    android:topLeftRadius="15dp"
    android:topRightRadius="15dp" />
</shape>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/rounded_textview">
        </TextView>

次の方法で実行時に色を変更すると:

TextView.setBackgroundColor();

使っていた形が消えます。適切な方法で変更するにはどうすればよいですか?それとも、色が異なるだけで多くの形状を生成する必要がありますか?

ありがとう。

4

3 に答える 3

1

色と半径の属性を含む PaintDrawable で解決策を見つけました。ただし、コンストラクターで色を設定する必要があります。そのため、毎回実行時に PaintDrawable を新しく作成し、それを TextView のバックグラウンド ドローアブルに設定する必要があります。

public static PaintDrawable getRoundedColorDrawable(int color, float radius, int padding) {
    PaintDrawable paintDrawable = new PaintDrawable(color);
    paintDrawable.setCornerRadius(radius);
    paintDrawable.setPadding(padding, padding, padding, padding);
    return paintDrawable;
}
于 2010-10-27T04:20:45.137 に答える
0

私は同じ問題を抱えていました

この方法を使用できます

TextView tv = // ... //;
tv.setBackgroundResource(R.drawable.myshape);

それは私にとってはうまくいきます!

于 2011-06-15T10:57:15.273 に答える
0

正しいソリッド要素を使用して、背景を別の形状に設定する必要があります。setBackgroundColor 次のようなものへの近道だと思います:

void setBackgroundColor(int color){
 ColorDrawable drawable = new ColorDrawable(color);
 setBackgroundDrawable(drawable);
}

そうです、いくつかの形状が必要になります:)

于 2010-10-26T18:33:43.137 に答える