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

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

问题描述

模拟

要求

我想用选择器放置自定义按钮.

I want to put custom button with selector.

上面给出了模型.

如果有人知道解决方案,请分享.

If anyone knows solution then share it.

谢谢.

推荐答案

基本上,您需要创建一些新的 XML 文件并将它们应用到您的 Button 元素.正如我从模型中看到的,您需要一个描边和应用了一些阴影效果的背景颜色,您可以对阴影进行更多研究,但背景颜色和描边非常简单.

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" />

希望这有帮助!

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

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