2013-03-03 139 views
2

我有一個按鈕,沒有任何文本只有背景顏色。在onClick()事件的按鈕我需要設置按鈕邊框沒有xml規範。我嘗試了漸變的矩形形狀作爲背景drawable到我的佈局不靈活的按鈕。Android按鈕邊框動態

如何設置按鈕的特定顏色邊框?

這是我的代碼。

Button btnBlackColor=new Button(this); 
    int mButtonWidth=100; 
    btnBlackColor.setWidth(mButtonWidth); 
    btnBlackColor.setHeight(mButtonWidth); 
    btnBlackColor.setBackgroundColor(Color.BLACK); 

    btnBlackColor.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
     GradientDrawable btnShape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLUE,Color.LTGRAY}); 
     btnShape.setCornerRadius(0); 
     btnShape.setSize(mButtonWidth, mButtonWidth); 
     btnShape.setBounds(10, 10, mButtonWidth, mButtonWidth); 
     ClipDrawable btnClip = new ClipDrawable(btnShape, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     btnShape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.BLACK, Color.DKGRAY}); 
     btnShape.setCornerRadius(0); 
     btnShape.setSize(mButtonWidth, mButtonWidth); 
     btnShape.setBounds(5, 5, mButtonWidth, mButtonWidth); 


     LayerDrawable btnLayer= new LayerDrawable(new Drawable[]{btnShape, btnClip}); 

     btnBlackColor.setBackgroundDrawable(btnLayer); 
     } 
    }); 
+0

「我有一個按鈕,沒有任何文本只有背景顏色」 - 這看起來或行爲都不像按鈕,因爲按鈕看起來響應點擊的是它的背景,即「StateListDrawable」。 – CommonsWare 2013-03-03 16:11:11

+0

如何在沒有xml規範的情況下添加StateListDrawable? – Karthick 2013-03-03 17:15:01

+0

'StateListDrawable'是一個Java類。歡迎您創建它的實例並根據您的需要配置這些實例。這就是說,因爲'StateListDrawable'的用戶中有99.44%是通過XML來完成的,你可能會發現通過Java管理一個例子的例子相對較少。 – CommonsWare 2013-03-03 17:44:35

回答

13

找到解決方案。

GradientDrawable drawable = new GradientDrawable(); 
    drawable.setShape(GradientDrawable.RECTANGLE); 
    drawable.setStroke(5, Color.MAGENTA); 
    drawable.setColor(Color.BLACK); 
    btnBlackColor.setBackgroundDrawable(drawable); 

使用這些值並設置爲按鈕背景可繪製。現在,按鈕以MAGENTA顏色的邊框顯示。

+0

對我來說也很有幫助。 +1 – 2013-04-16 05:47:34

+0

我如何設置圖像,然後添加筆畫? – 2014-01-09 09:05:41

0

在xml背景中使用選擇器。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"><!-- pressed --> 
     <shape>...</shape> 
    <item android:state_focused="true" 
          android:drawable="@drawable/button_focused" /> <!-- focused --> 
    <item android:state_hovered="true" 
          android:drawable="@drawable/button_focused" /> <!-- hovered --> 
    <item android:drawable="@drawable/button_normal" /> <!-- default --> 
</selector> 

伯頓的每一個州可能是一個形狀,外觀屬性,你可以修改:

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape=["rectangle" | "oval" | "line" | "ring"] > 
    <corners 
        android:radius="integer" 
        android:topLeftRadius="integer" 
        android:topRightRadius="integer" 
        android:bottomLeftRadius="integer" 
        android:bottomRightRadius="integer" /> 
    <gradient 
        android:angle="integer" 
        android:centerX="integer" 
        android:centerY="integer" 
        android:centerColor="integer" 
        android:endColor="color" 
        android:gradientRadius="integer" 
        android:startColor="color" 
        android:type=["linear" | "radial" | "sweep"] 
        android:useLevel=["true" | "false"] /> 
    <padding 
        android:left="integer" 
        android:top="integer" 
        android:right="integer" 
        android:bottom="integer" /> 
    <size 
        android:width="integer" 
        android:height="integer" /> 
    <solid 
        android:color="color" /> 
    <stroke 
        android:width="integer" 
        android:color="color" 
        android:dashWidth="integer" 
        android:dashGap="integer" /> 
</shape> 

通過代碼,你可以使用一個StateListDrawable做出選擇,對於形狀使用DrawableShape請看ARPT這些鏈接:

http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

http://developer.android.com/reference/android/graphics/drawable/ShapeDrawable.html

+0

我需要設置這個沒有XML文件。我怎樣才能設置這些值? – Karthick 2013-03-03 16:47:43

+0

你到底需要做什麼? setBackgroundDrawable對你沒有用處? – vgarzom 2013-03-03 17:21:47

+0

單擊按鈕時,我如何突出顯示該按鈕?我更喜歡它可能是一個邊界。 – Karthick 2013-03-04 03:32:06