微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在android中创建带选择器的自定义形状按钮?

小样

需求

我想把自定义按钮与选择器.

上面给出了模拟.

如果有人知道解决方案然后分享它.

谢谢.

解决方法:

基本上你需要创建一些新的XML文件并将它们应用到你的Button元素.正如我从模型中看到的那样,你需要一个笔划和应用了一些阴影效果的背景颜色,你可以更多地研究阴影,但背景颜色和笔触非常直接.

这是一个例子,done_rounded_btn.xml:

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_pressed="true" 
        android:state_enabled="true" 
        android:drawable="@drawable/zzzzzzzzz_btn_orange" />
    <item 
        android:state_focused="true" 
        android:state_enabled="true"
        android:drawable="@drawable/zzzzzzzzz_btn_orange" />
    <item 
        android:state_focused="false" 
        android:state_enabled="false"
        android:drawable="@drawable/zzzzzzzzz_btn_inactiv" />
    <item android:drawable="@drawable/zzzzzzzzz_btn_black"/>
</selector>

对于选择部分,然后创建与模型对应的自定义drawable.

一个例子,zzzzzzzzzz_btn_orange:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid
        android:color="@color/done_color">
    </solid>

    <corners
        android:bottomLefTradius="3dp"
        android:bottomrighTradius="3dp"
        android:topLefTradius="3dp"
        android:topRighTradius="3dp" />

</shape>

然后将其添加到按钮作为后台main.xml:

<Button
            android:id="@+id/registers_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/done_rounded_btn"
            android:text="@string/done_txt"
            android:textColor="@color/white"
            android:textSize="15sp" />

希望这可以帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐