19

背景色として必要な色を持つ単純なDrawableオブジェクトを作成する必要がありますが、XML スキーマを使用せずにプログラムでそれを行う方法がわかりません (本当に可能ですか?)。教えてください、私はそれを作成しLayerDrawableて作業するために必要ですSeekBar(プログラムの背景を変更しSeekBarます)。前もって感謝します。

4

3 に答える 3

45

ColorDrawableを使用してみてください。コンストラクターを使用して色で構築できますColorDrawable(int color)

于 2013-01-16T12:16:51.253 に答える
7

@Thrakbad で提案されているように、次を使用できますColorDrawable

    TextView textView = new TextView(this);
    ColorDrawable colorDrawable = new ColorDrawable(0xFFFF0000);
    textView.setBackgroundDrawable(colorDrawable);
于 2016-05-18T12:59:54.840 に答える
6

ColorDrawableはあなたのケースであなたを助けます、あなたはあなたのドローアブルのためにパラメータ色を渡すことができます。

または、次のようなことを行うことができます。

ImageView imgStatus = (ImageView) findViewById(R.id.imgInfoIcon);
// Load the icon as drawable object
Drawable d = getResources().getDrawable(R.drawable.ic_menu_info_details);

// Get the color of the icon depending on system state
int iconColor = android.graphics.Color.BLACK
if (systemState == Status.ERROR)
    iconColor = android.graphics.Color.RED
else if (systemState == Status.WARNING)
    iconColor = android.graphics.Color.YELLOW
else if (systemState == Status.OK)
    iconColor = android.graphics.Color.GREEN

// Set the correct new color
d.setColorFilter( iconColor, Mode.MULTIPLY );

// Load the updated drawable to the image viewer
imgStatus.setImageDrawable(d);

上記のコードはもともとここに投稿されています

于 2013-01-16T12:22:40.930 に答える