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

如何在android中使用选择器创建自定义形状按钮?,android,button,Android,Button,实体模型 要求 我想使用选择器设置自定义按钮 实物模型如上所示 如果有人知道这个解决方案,那就分享吧 谢谢。基本上,您需要创建一些新的XML文件,并将它们应用于按钮元素。正如我从模型中看到的,你需要一个笔划和背景色,并应用一些着色效果,你可以对着色进行更多的研究,但是背景色和笔划是非常直接的 下面是一个示例,done_rounded_btn.xml: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:and

实体模型


要求


我想使用选择器设置自定义按钮

实物模型如上所示

如果有人知道这个解决方案,那就分享吧


谢谢。

基本上,您需要创建一些新的XML文件,并将它们应用于按钮元素。正如我从模型中看到的,你需要一个笔划和背景色,并应用一些着色效果,你可以对着色进行更多的研究,但是背景色和笔划是非常直接的

下面是一个示例,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>

然后创建与模型对应的自定义绘图

例如,zzzzzz_btn_橙色:

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


希望这有帮助

您可以使用此按钮而不是标准按钮,并将选择器设置为xml中的背景:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

/**
 * Custom Shape Button which ignores touches on transparent background.
 */
public class ButtonWithUntouchableTransparentBg extends Button {

    public ButtonWithUntouchableTransparentBg(Context context) {
        this(context, null);
    }

    public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setDrawingCacheEnabled(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        // ignores touches on transparent background
        if (isPixelTransparent(x, y))
            return true;
        else
            return super.onTouchEvent(event);
    }

    /**
     * @return true if pixel from (x,y) is transparent
     */
    private boolean isPixelTransparent(int x, int y) {
        Bitmap bmp = Bitmap.createBitmap(getDrawingCache());
        int color = Color.TRANSPARENT;
        try {
            color = bmp.getPixel(x, y);
        } catch (IllegalArgumentException e) {
            // x or y exceed the bitmap's bounds.
            // Reverts the View's internal state from a previously set "pressed" state.
            setPressed(false);
        }

        // Ignores touches on transparent background.
        if (color == Color.TRANSPARENT)
            return true;
        else
            return false;
    }
}

在项目内部,将形状放入选择器XML中

我的代码中的EX:

<!-- if pressed -->
<item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle">
        <solid android:color="@color/blue" />

        <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
    </shape></item>

<!-- if not pressed -->
<item><shape android:padding="10dp" android:shape="rectangle">
        <solid android:color="@color/Purbble" />

        <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />
    </shape></item>



您还可以创建一个内部使用选择器的形状。如果你的形状只是在不同的状态下改变它的颜色,这会更干净

color/color\u selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue_dark" android:state_pressed="true" />
    <item android:color="@color/blue_light" />
</selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/color_selector" />
    <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
    <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>

drawable/shape.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/blue_dark" android:state_pressed="true" />
    <item android:color="@color/blue_light" />
</selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/color_selector" />
    <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
    <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>

圆角按钮有两种状态(启用/禁用):



我有两个朝这个方向的PNG。那么,我如何旋转(倾斜)按钮呢?好吧,你将从头开始重新创建PNG的效果(至少我会这么做)。使用颜色选择器(如Mozilla上的附加组件)获取按钮每个状态的颜色代码,将其添加到绘图表中,然后添加笔划效果。我不确定阴影效果,但您可以在StackOverflow上查看它所有的z是什么?没有特别的原因,可以随意命名,但您认为最好:)很明显,但我必须说,这个解决方案支持选择器,所以只需以通常的方式为按钮添加几个状态图像(使用selector.xml)如果使用api>11,可以使用rotateX和rotateY方法来实现这种视图。需要定义颜色属性