2017-03-02 138 views
1

我創建了一個按鈕,邊框:更改按鈕的背景,邊框

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 

    <solid android:color="#FFFFFFFF" /> 

    <stroke 
     android:width="1dp" 
     android:color="#FFCCCCCC" /> 
</shape> 

<Button 
     android:text="@null" 
     android:stateListAnimator="@null" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:id="@+id/colorButton" 
     android:background="@drawable/button_border" /> 

現在我改變編程背景的顏色。問題是,只要我更改背景,邊框就會被移除。有沒有辦法改變按鈕的背景顏色並保持邊框?

回答

3

試試這個,

Button colorButton = (Button) findViewById(R.id.colorButton); 
    GradientDrawable background = (GradientDrawable) colorButton.getBackground(); 
    background.setColor(getResources().getColor(R.color.some_color)); 
+0

工程就像一個魅力。謝謝! – Patricks

+0

不客氣:) – user2025187

+0

給我很漂亮的方向挖:)非常好! – LPVOID

2

使用下面的代碼

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 

    <solid android:color="#FFFFFFFF" /> 

    <stroke 
     android:width="1dp" 
     android:color="#FFCCCCCC" /> 
<gradient 
     android:startColor="@color/white" 
     android:centerColor="@color/white" 
     android:endColor="@color/white"/> 

</shape> 

對於更改色彩動態地使用下面的代碼。

Drawable background = yourView.getBackground(); 
    if (background instanceof ShapeDrawable) { 
     // cast to 'ShapeDrawable' 
     ShapeDrawable shapeDrawable = (ShapeDrawable) background; 
     shapeDrawable.getPaint().setColor(getResources().getColor(R.color.colorToSet)); 
    } else if (background instanceof GradientDrawable) { 
     // cast to 'GradientDrawable' 
     GradientDrawable gradientDrawable = (GradientDrawable) background; 
     gradientDrawable.setColor(getResources().getColor(R.color.colorToSet)); 
    } else if (background instanceof ColorDrawable) { 
     // alpha value may need to be set again after this call 
     ColorDrawable colorDrawable = (ColorDrawable) background; 
     colorDrawable.setColor(getResources().getColor(R.color.colorToSet)); 
    } 
+0

的問題是,在資源文件中的按鈕的背景設置與繪製和編程方式更改背景btn.setBackgroundColor(Color.parseColor(「#FF0000」)); 所以我必須改變形狀的固體顏色,我不知道該怎麼做 – Patricks

0

也許你應該嘗試,並獲得了背景繪製的引用,然後塗抹一些顏色,如下面的代碼我張貼:

GradientDrawable gradientDrawable = (GradientDrawable) colorButton.getBackground(); 
    gradientDrawable.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC); 

其中#FF0000是要顯示新的顏色。 這樣,我認爲邊框不會被刪除。

0

在同樣的drawable用途上應用新顏色DrawableCompat class。

DrawableCompat.setTintList(d,drawableTintColor); // d is drawable object and drawableTintColor is color you want to apply 
1

從我的觀點沒有必要建立對自定義繪製點只需要添加任何佈局應用保證金android:layout_margin="1dp"到您的RelativeLayout.Now按鈕和android:background="#FF4081"只是改變背景你的屁股。

<RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#FF4081"> 
      <Button 
       android:id="@+id/colorButton" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:layout_margin="1dp" 
       android:background="@android:color/white" 
       android:text="@null" /> 
     </RelativeLayout> 
+0

這也是一個非常整潔的方式。我早就應該想到這一點。謝謝 – Patricks