【问题标题】:android button with double border and gradient带有双边框和渐变的android按钮
【发布时间】:2013-10-17 12:20:11
【问题描述】:

我想创建一个自定义按钮。 这个按钮应该有一个渐变和一个两个像素的边框,但内边缘和外边缘应该是不同的颜色(例如:内是红色,外是黄色)。

我的问题:如何对双边框进行编程(如图所示)?!

图片:

我尝试了一个带有两个笔画的 XML 文件,但它不起作用。

我可以用 9png 的文件来做这件事,但我想用纯编码来做。

【问题讨论】:

  • 感谢您插入图片。我没有足够的代表。

标签: java android button border


【解决方案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>

将上面的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>

结果:

【讨论】:

  • 太棒了!我看得太远了。谢谢老兄:)
  • @Dancger 我很高兴它帮助了你。
【解决方案2】:

如果你想使用纯 Java 代码,那么你需要创建一个扩展按钮的类,将所有逻辑写在

public void onDraw(Canvas iCanvas).

我从我的一个项目中粘贴了小代码 sn-p。试试看。以为我没有创建渐变,我使用的是纯色。

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 等)而没有媒体(例如:图像)。但是谢谢你,我相信我将来会需要这个代码。
【解决方案3】:

将按钮放在您将为它创建的布局中。因此,将布局设置为您想要的最外层背景颜色。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多