Android双边框和渐变按钮

8
我想创建一个自定义按钮。该按钮应具有渐变和两个像素的边框,但内部和外部边缘应该是不同颜色的(例如:内部为红色,外部为黄色)。
我的问题是:如何编写双重边框(如图所示)?
图片: Example Button Image 我尝试使用包含两个描边的XML文件,但它不起作用。
我可以使用9png文件完成此操作,但我想要通过纯编码实现。

感谢您插入图片。我没有足够的声望。 - Dancger
3个回答

16

btn_bg.xml

 <?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item > 
    <shape android:shape="rectangle">
                
       <padding android:left="3.5px"
            android:top="3.5px"
            android:right="3.5px"
            android:bottom="3.5px"/>
                    
       <solid android:color="#d4e23a"/>
                
    </shape>
</item>
<item > 
    <shape android:shape="rectangle">
                
       <padding android:left="4.5px"
            android:top="4.5px"
            android:right="4.5px"
            android:bottom="4.5px"/>
                    
        <solid android:color="#d4413a"/>
            
    </shape>
</item>
<item > 
    <shape android:shape="rectangle">
        <gradient android:startColor="#37c325"
            android:endColor="#2573c3"
            android:angle="-90"/>
                    
    </shape>
</item>


</layer-list>

将上述XML设置为按钮背景。

           <Button
                
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=""
                android:background="@drawable/btn_bg"
                android:gravity="center"
                android:padding="10dp"
                android:textStyle="bold"
                >
                           
           </Button>

结果:

图像


1
@Dancger,我很高兴它能帮到你。 - TheFlash

1
如果您想使用纯Java代码,则需要创建一个继承按钮的类,并在public void onDraw(Canvas iCanvas)中编写所有逻辑。我从我的一个项目中粘贴了一小段代码片段。试试看。虽然我没有创建渐变,但我使用了纯色。
public class MyButton extends Button {

    private Paint m_paint1 = new Paint();
    private Paint m_paint2 = new Paint();
    private int m_color1 = 0XFF92C84D; // LIKE AN OLIVE GREEN..
    private int m_color2 = 0XFFFF0000; // LIKE AN OLIVE GREEN..

    private RectF innerRect1, innerRect2;

    public MyButton(Context context) {
        super(context);
        setBackgroundColor(Color.BLACK);

    }

    public void onDraw(Canvas iCanvas) {
        // draw the button background
        m_paint1.setColor(m_color1);
        m_paint2.setColor(m_color2);
        innerRect1 = new RectF(5, 5, getWidth() - 5, getHeight() - 5);
        innerRect2 = new RectF(10, 10, getWidth() - 10, getHeight() - 10);
        iCanvas.drawRoundRect(innerRect1, 0, 0, m_paint1);
        iCanvas.drawRoundRect(innerRect2, 0, 0, m_paint2);
    }

    public static RelativeLayout.LayoutParams GetRelativeParam(int iLeft,
            int iTop, int iWidth, int iHeight) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                iHeight, iWidth);
        params.leftMargin = iLeft;
        params.topMargin = iTop;
        return params;
    }
}

RelativeLayout relLay = new RelativeLayout(this);

        MyButton m_button = new MyButton(this);
        setContentView(relLay);

        relLay.addView(m_button, MyButton.GetRelativeParam(0, 0, 100, 500));

抱歉拼错了,我的意思是只有代码(XML、Java等),没有媒体文件(例如图像)。但还是谢谢你,我相信将来我会需要这段代码。 - Dancger

0
将按钮放置在您专门为其创建的布局中。因此,将所需的最外层背景颜色设置为该布局。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接