[英]How to create a custom shape of a button but keep it clickable and focusable
我想制作一个具有自定义形状的按钮,但是当我尝试更改形状时,却无法点击/聚焦,我该怎么做?
I hope this is what you are looking for, but I have done this a couple times where I liked a rounded button a little bit more, or if it's a widget, I wanted a checkbox.我希望这就是你要找的东西,但我已经做了几次,我更喜欢圆形按钮,或者如果它是一个小部件,我想要一个复选框。 Here's a couple examples I just did by just changing android:background:
这是我刚刚通过更改 android:background 所做的几个示例:
<Button
android:id="@+id/widget_yes_no_checkbox_off"
android:layout_width="@dimen/checkbox_size"
android:layout_height="@dimen/checkbox_size"
android:layout_marginEnd="@dimen/right_margin"
android:layout_gravity="center"
android:background="@android:drawable/checkbox_off_background"
/>
This gave me a button that looks like a checkbox (necessary for making a checkbox in a widget).这给了我一个看起来像复选框的按钮(在小部件中创建复选框所必需的)。
Or, here's a drawable xml to help me round a button或者,这是一个可绘制的 xml 来帮助我围绕按钮
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="@color/primaryDarkColor"/>
<corners android:radius="20dp"/>
</shape>
with it's corresponding style theme:与其相应的风格主题:
<style name="myAppStyle.ButtonStyle">
<item name="android:background">@drawable/rounded_button</item>
</style>
So, in summary, I think the easiest way to do this is to still do a button layout, but use the background attribute to change the way your button looks.所以,总而言之,我认为最简单的方法是仍然进行按钮布局,但使用背景属性来更改按钮的外观。
You can do it by two ways,你可以通过两种方式做到这一点
Set any layout/button with shape drawable and add android:foreground="?attr/selectableItemBackground"
in XML设置任何具有可绘制形状的布局/按钮并在 XML 中添加
android:foreground="?attr/selectableItemBackground"
Set the Style attribute in button.在按钮中设置样式属性。
style="@style/buttonTheme"
in its XML and define <style name="buttonTheme" parent="Widget.AppCompat.Button.Colored"> <item name="colorButtonNormal">@color/#000000</item>
in /style folder. style="@style/buttonTheme"
在其 XML 中定义<style name="buttonTheme" parent="Widget.AppCompat.Button.Colored"> <item name="colorButtonNormal">@color/#000000</item>
/style 文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.