Android 中带有背景颜色的圆角按钮
我需要在android中做圆角按钮并改变背景颜色。
我怎么能这么做呢?
示例链接/代码非常感谢。
I need to do round cornered button with background color change in android.
How could i do that?
Example link/code is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要使用 Android 的 Shape Drawables。
http://developer.android.com/guide/topics/resources /drawable-resource.html#Shape
drawable/cool_button_background.xml
然后您必须从这些形状可绘制对象创建一个“选择器”可绘制对象。这允许您根据状态使按钮显示不同。 IE:按下、聚焦等。
http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList
drawable/cool_button.xml
额外好处:您可能想要为按钮创建一个样式,以便它们在整个程序中保持一致。您可以省略这一步,只需设置按钮的 android:background="@drawable/cool_button" 即可。
value/styles.xml
最后是按钮!
You want to use Android's Shape Drawables.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
drawable/cool_button_background.xml
Then you'd have to create a "selector" drawable from those shape drawables. This allows you to make the button appear different depending on the state. IE: Pressed, focused, etc.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
drawable/cool_button.xml
Bonus: You might want to create a style for the button so you can have them be consistent throughout the program. You can cut this step out and just set the button's android:background="@drawable/cool_button".
values/styles.xml
Finally, the button!
导入 PorterDuff 并使用 setColorFilter() 如下
Import PorterDuff and use setColorFilter() as follows