2017-04-08 119 views
0

我想以編程方式更改Button背景色按鈕 shape(oval)。我改變了XML。以編程方式更改橢圓形按鈕背景

這是代碼:

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <corners android:radius="20dp"/> 
     <solid android:color="#f9f9f9"/> 
     <stroke 
      android:width="2dp" android:color="#FFFF4917" /> 
      </shape> 

我使用Button.backgroundColor(COLOR.RED)改變這種Button但問題是,它改變按鈕的顏色,但它使我的按鈕矩形(默認設置形狀按鈕)

回答

1

你可以使用這個簡單的方法向下跌破

GradientDrawable BackgroundShape = (GradientDrawable)btn.getBackground(); 
BackgroundShape.setColor(Color.BLACK); 
修改
+0

感謝working.will 10分鐘後投它 – Meghna

+0

編碼愉快:)順便說一下我編輯你的問題,你已經在你原來做的拼寫錯誤。 – Rab

0

使用它。當點擊按鈕時,它會讓你的橢圓形的形狀。

style_login_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
     <shape android:shape="oval"> 
      <solid android:color="@color/colorDeepOrange"/> 
      <size android:width="120dp" android:height="120dp"/> 
     </shape> 
    </item> 
    <item android:state_focused="true"> 
     <shape android:shape="oval"> 
      <solid android:color="@color/colorOrange"/> 
      <size android:width="120dp" android:height="120dp"/> 
     </shape> 
    </item> 
    <item > 
     <shape android:shape="oval"> 
      <solid android:color="@color/colorOrange"/> 
      <size android:width="120dp" android:height="120dp"/> 
     </shape> 
    </item> 
</selector> 

,並適用於Button。像 -

<Button 
       android:id="@+id/btnSignin" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/text_btn_login" 
       android:textColor="@color/colorWhite" 
       android:textSize="25dp" 
       android:padding="30dp" 
       android:textAllCaps="false" 
       android:layout_marginTop="20dp" 
       android:layout_marginBottom="20dp" 
       android:background="@drawable/style_login_button"/> 
+0

他想要使用代碼以編程方式將按鈕設置爲橢圓形。 – Rab

相關問題