2017-04-14 9 views
5

ボタンのdrawableLeft/drawableRightの色をプログラムで変更する方法を解明しようとしています。働く下に述べたように、私は>私のXMLで描画可能色合いを使用していた23プログラムレベルでのAPIの下のボタンの描画可能な色合いをプログラムで変更する方法

<Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="VIEW ALL" 
       android:layout_centerInParent="true" 
       android:background="#00000000" 
       android:drawableLeft="@mipmap/ic_menu_black_36dp" 
       android:layout_centerVertical="true" 
       android:id="@+id/view_all" 
       android:textColor="@color/bottom_color" 
       android:drawableTint="@color/bottom_color" 
       /> 
     Button prev = (Button) findViewById(R.id.prev); 

    Drawable[] drawables =prev.getCompoundDrawables(); 
     drawables[0].setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY); 
     prev.setCompoundDrawables(drawables[0],null,null,null); 

ソリューションカラー< APIレベルを変更することができるAPIレベル23ではない:あなたはPorterDuff.Mode.MULTIPLYを使用している

Drawable[] drawablesprev =prev.getCompoundDrawables(); 

//for drawableleft drawable array index 0 

    drawablesprev[0].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 

//for drawableright drawable array index 2 
drawablesprev[2].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 


//for drawabletop drawable array index 1 
    drawablesprev[1].setColorFilter(getResources().getColor(R.color.assessment_bottom), PorterDuff.Mode.SRC_ATOP); 
+0

'android.support.v4.graphics.drawable.DrawableCompat'とその' setTint(Drawable drawable、int tint) 'メソッド – pskink

+0

を使用すると、同じサンプルコードを提供できますか?私は今ここにこだわっています –

+0

何にこだわったのですか?あなたのコードは何ですか? – pskink

答えて

0

、あなたは色を増やしています。あなたのアイコンが黒であると仮定すると、#000000またはintとして0となります。 0 * GRAY(または他の色)はいつもあなたに0を与えますので、まだ黒です...

お試しくださいPorterDuff.ModePorterDuff.Mode.SRC_ATOPまたはPorterDuff.Mode.SRC_IN

あなたの現在のコードは、おそらくここでMULTIPLY

+0

を使用して、getCompoundDrawablesを使用していました –

0

で適切に色付けされなければならない、アイコン、白バージョンで動作しますが、あなたのTextViewまたはボタン描画可能に色付けする簡単な方法です:

private void tintViewDrawable(TextView view) { 
     Drawable[] drawables = view.getCompoundDrawables(); 
     for (Drawable drawable : drawables) { 
      if (drawable != null) { 
       drawable.setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_ATOP); 
      } 
     } 
    } 
関連する問題