android button with double border and gradient

拟墨画扇 提交于 2020-06-24 12:15:01

问题


I want to create a custom button. This button should have a gradient and a two pixel border, but the inner and outer edge should be in a different color (example: inner is red and outer is yellow).

My question: how do I program the double border (like in the image)?!

Image:

Example Button Image

I tried with an XML file with two strokes, but it doesn't work.

I could do this with a 9png file, but I want to do it with pure coding.


回答1:


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>

set the above xml as button background.

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

Result:

Image




回答2:


If you want to go by plain Java code then you need to create a class which extends the button, write all your logic in

public void onDraw(Canvas iCanvas).

I have pasted small code snippet from one of my project. Give it a try. thought I have not created the gradient, I have used plain colors.

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;
    }
}

and

RelativeLayout relLay = new RelativeLayout(this);

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

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



回答3:


Put the button inside a layout that you will create just for it. So set to the layout the outest background color that you want.



来源:https://stackoverflow.com/questions/19426595/android-button-with-double-border-and-gradient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!