2013-10-17 14 views
5

我想創建一個自定義按鈕。 這個按鈕應該有一個漸變和一個兩個像素的邊框,但內邊和外邊應該是不同的顏色(例如:內部是紅色的,外部是黃色的)。帶雙邊框和漸變的android按鈕

我的問題:我如何編程雙邊框(如圖像中)?

圖片:

Example Button Image

我試着用兩招一個XML文件,但它不工作。

我可以用9png文件做到這一點,但我想用純編碼來做到這一點。

+0

由於用於插入圖像。我沒有足夠的代表。 – Dancger

回答

13

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> 

結果:

Image

+0

太棒了!我看起來太遠..謝謝你的傢伙:) – Dancger

+1

@Dancger我很高興它幫助你。 – TheFlash

0

將按鈕放入您將爲其創建的佈局中。因此,設置爲您想要的最簡單背景顏色的佈局。

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)); 
+0

對不起,拼寫錯誤。我的意思是僅僅使用代碼(xml,java等)而沒有使用媒體(例如:image)。 但是,爲此,我相信我將來需要這些代碼。 – Dancger