2016-06-08 31 views
2

我有具有背景繪製作爲按鈕一個xml:setColorFilter到按鈕不改變行程

<shape android:shape="rectangle"> 
      <corners android:bottomRightRadius="20dp" 
       android:topLeftRadius="20dp"/> 
      <stroke android:width="1dp" android:color="@color/black"/> 
</shape> 

我想僅編程來改變按鈕的背景的顏色。 所以我想這一點,

button.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.red), PorterDuff.Mode.SRC_ATOP); 

不過,我看不出中風在所有的時候我setColorFIlter()。 任何輸入?

回答

0

這不是你的問題的最佳解決方案,但它肯定

RippleDrawable rippleImgStart; 
Drawable imgStart; 
final int ColorWhiteOpacity = Color.argb(75,255,255,255); 
imgStart = ContextCompat.getDrawable(this, R.drawable.start); 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
      rippleImgStart = new 
        RippleDrawable(ColorStateList.valueOf(ColorWhiteOpacity), imgStart, null); 
      btn.setImageDrawable(rippleImgStart); 
} 

工作這基本上是使用圖像提供了漣漪效應,這樣你們可以達到連鎖反應也..

和ColorWhiteOpacity您可以使用您的顏色並將其設置爲圖像。

在這裏你可以找到例子:link