我想设置按钮背景的ColorFilter。以下是我所做的:
Drawable buttonBackground = ContextCompat.getDrawable(this, R.drawable.shape_rect_stroke);
buttonBackground.setColorFilter(ContextCompat.getColor(this, R.color
.colorPrimary), PorterDuff.Mode.ADD);
btnSignIn.setBackground(buttonBackground);shape_rect_stroke.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/colorWhite"/>
<solid android:color="@android:color/transparent"/>
</shape>通过这样做,我得到了,

但我想要这个:

我的xml文件运行良好,但我希望以务实的方式更改stroke颜色,使solid颜色保持透明。所以我可以对不同的颜色按钮背景使用相同的xml ..。
请给我建议我怎样才能拿到这个。谢谢!
发布于 2016-04-28 11:32:09
最后,我这样做了:
GradientDrawable drawable = (GradientDrawable) btnSignIn.getBackground();
drawable.setStroke(3, ContextCompat.getColor(this,R.color.colorPrimary));发布于 2016-04-28 11:03:12
为我工作的shape_rect_stroke.xml 文件下面使用的透明按钮和边框:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<!--make button border solid color, nontransparent-->
<stroke android:color="#ffffff" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
</item>
</selector>如果您想以编程方式进入,请参见下面的代码
GradientDrawable gd = new GradientDrawable();
gd.setColor(0x00FFFFFF); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFFFFFFFF);
btnSignIn.setBackgroundDrawable(gd);发布于 2016-04-28 11:06:46
尝尝这个,
findViewById(R.id.button).setBackground(setBackgroundDrawable("#3F51B5"));创建方法来设置Stroke运行时:
private GradientDrawable setBackgroundDrawable(String strokeColor){
GradientDrawable shape = new GradientDrawable();
shape.setStroke(2, Color.parseColor(strokeColor));
shape.setColor(Color.parseColor("#80000000"));
return shape;
}https://stackoverflow.com/questions/36912584
复制相似问题