繁体   English   中英

如何在Java中以编程方式创建具有圆形边缘的按钮?

[英]How to create a button with round edges programatically in Java?

是否可以在我的可绘制文件夹中不使用形状XML的情况下创建带有圆形边缘的按钮?

我的应用程序有很多按钮,这些按钮会根据用户分配和触摸的内容来改变颜色。 这意味着我将必须在xml中为其特定颜色(具有大约20种颜色)创建很多形状。

还是有一种方法可以轻松更改形状按钮的背景颜色? 我的应用程序的一页上有大约45个按钮。

您可以使用imageView.setColorFilter(Color.RED)函数更改xml drawable的颜色

这是一些例子

ImageView redCircle = (ImageView) findViewById(R.id.circle_red_imageview);
ImageView greenCircle = (ImageView) findViewById(R.id.circle_green_imageview);
ImageView blueCircle = (ImageView) findViewById(R.id.circle_blue_imageview);

// we can create the color values in different ways:
redCircle.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY );
greenCircle.getDrawable().setColorFilter(0xff00ff00, PorterDuff.Mode.MULTIPLY );
blueCircle.getDrawable().setColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM