如何使用选择器在Android中创建自定义形状的按钮? [英] How to create custom shape button with selector in android?

查看:134
本文介绍了如何使用选择器在Android中创建自定义形状的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小样

要求

我想提出的自定义按钮是选择

I want to put custom button with selector.

小样上面给出了。

如果有人知道的解决方案,然后分享它。

If anyone knows solution then share it.

感谢你。

推荐答案

基本上你需要创建一些新的XML文件,并将它们应用到你的Button元素。我可以从样机看你会需要一个行程,并应用了一些遮阳效果的背景颜色,你可以研究更进了阴影的事情,但背景颜色和行程是pretty的直线前进。

basically you will need to create some new XML files and apply them to your Button element. As i can see from the mockup you will need a stroke and the background color with some shading effect applied, you can research more into the shading thing but the background color and the stroke is pretty straight forward.

下面是一个例子,done_rounded_btn.xml:

Here is an example, 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>

在选择的一部分,然后创建相应的样机自定义可绘制。

for the selection part and then you create the custom drawables corresponding to the mockup.

这是例子,zzzzzzzzzz_btn_orange:

An example, 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中:

And then add it to your button as background, 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" />

希望这有助于!

Hope this helps!

这篇关于如何使用选择器在Android中创建自定义形状的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆