2017-12-26 5 views
2

私のdrawablesフォルダにこのxmlがあります。描画可能なセットの背景/色をビューの背景として変更するにはどうすればよいですか?

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners android:radius="10dp"/> 
    <solid android:color="#ffffff"/> 
</shape> 

私は基本的にこれをバックグラウンドとして設定したビューに対して丸い角を付けるためにこれを使用しています。しかし、私が知りたいのは、そのビューの色/背景色合いを動的に保つことができることです。私は3つのビューを持っていると言うことができますが、すべての角を丸くする必要がありますが、すべて別の色である必要がある分、私は別々の3つのファイルを作成し、各ファイルを個別に色を設定する必要があります。私が上記のファイルで行ったように。元のビューでBackgroundTintを使用しようとしましたが、動作しません。

+0

を。 – FarshidABZ

答えて

3
GradientDrawable background = (GradientDrawable) textView.getBackground(); 
background.setColor(getResources().getColor(R.color.some_color)); 

私はあなたの形状drawableをbackgroundとしてtextViewに適用したと考えています。

背景を取得するために背景を設定したビューを使用する必要があります。

2

はDrawableの背景を作成し、ビューの背景設定:あなたは、実行時に描画可能を作成し、それをあなたのビューの背景を設定することができます

private void setBackground(View v, int backgroundColor) { 
    GradientDrawable shape = new GradientDrawable(); 
    shape.setShape(GradientDrawable.RECTANGLE); 
    shape.setColor(backgroundColor); 
    shape.setSize(width, height); 
    shape.setCornerRadius(4.0f); 
    v.setBackground(shape); 
    v.setBackgroundDrawable(shape); 
} 
関連する問題