2014-12-11 64 views
36

我想使用使用按鈕背景自定義顏色,而使用selectableItemBackground屬性

android:background="?android:attr/selectableItemBackground"

讓我的按鈕爲每個Android版本一樣盪漾做適當的效果等

但當我需要不同的顏色時,這會產生一個灰色的按鈕。

如何覆蓋此默認顏色?

+0

您是否找到了解決方案? – rafid059 2015-03-24 18:00:19

+0

同樣在這裏,試圖將其撞擊到2016. – 2016-06-22 13:57:45

回答

10

如果您閱讀source code of Button.java,則會看到它是TextView.java的一個子類。我已針對問題提出了一個簡單的解決方法。

<LinearLayout android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:clickable="true" 
       android:background="#1f5050"> 

     <TextView android:layout_width="some_dp" 
       android:layout_height="some_dp" 
       android:id="@+id/button" 
       android:background="?android:selectableItemBackground" /> 

</LinearLayout> 

在代碼:

button.setOnClickLisetener(new Onclicklistener() { 
    // do your stuff here 
} 

它會好得多,如果有人可以擴展的TextView類,並自定義按鈕與有問題的功能。

注:我minsdk是14.此外,棒棒糖連鎖反應工作得很好

-1

而不是使用的機器人:ATTR/selectableItemBackground您可以創建繪製與以下內容的XML文件夾中。

<item 
    android:state_pressed="true" 
    android:drawable="@color/orange"/> 
<item 
    android:state_enabled="false" 
    android:drawable="@color/default"/> 
<item 
    android:drawable="@color/default"/> 

</selector> 
Updated: so you save this files as : btn_drawable.xml in drawable folder. 

Simply replace ?android:attr/selectableItemBackground with *@drawable/btn_drawable.xml* 
+13

這不會爲每個Android版本的適當效果如漣漪等。「如問題 – 2015-09-09 03:01:07

-2

編輯:這是現在可以使用程序兼容性和backgroundTint

android:backgroundTint="@color/yourColor" 

以前的解決方案:

我有同樣的問題,並最終以編程方式這樣做:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     ColorStateList colors = new ColorStateList(new int[][]{ 
       new int[]{android.R.attr.state_enabled}, 
     }, new int[]{pressed}); 
     GradientDrawable item = new GradientDrawable(); 
     item.setCornerRadius(radius); 
     item.setColor(normal); 
     RippleDrawable ripple = new RippleDrawable(colors, item, null); 
     button.setBackgroundDrawable(ripple); 
    } else { 
     StateListDrawable stateListDrawable = new StateListDrawable(); 
     GradientDrawable item; 
     item = new GradientDrawable(); 
     item.setCornerRadius(radius); 
     item.setColor(pressed); 
     stateListDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, item); 
     item = new GradientDrawable(); 
     item.setCornerRadius(radius); 
     item.setColor(normal); 
     stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, item); 
     button.setBackgroundDrawable(stateListDrawable); 
    } 
+9

backgroundTint似乎並沒有改變我的背景顏色。 – 2016-02-01 21:53:58

4

只需在外部/父母級別移動所需的顏色,例如「@ color/Red」是按鈕顏色:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/Red" 
     android:layout_weight="1"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/selectableItemBackground" 
      android:gravity="center" 
      android:text="Hello" 
      android:textColor="@color/White"/> 
    </LinearLayout> 
0

使用此方法,您可以自定義漣漪效果顏色。首先,您必須在可繪製資源目錄中創建一個xml文件。創建一個ripple_effect.xml文件並添加以下代碼。我使用背景色作爲紅色。

RES /繪製/ ripple_effect.xml

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:color="#af0c0e" 
    tools:targetApi="lollipop"> 
    <item android:id="@android:id/mask"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#af0c0e" /> 
     </shape> 
    </item> 
</ripple> 

並設置背景上面繪製資源文件。 xml佈局活動的最終代碼如下所示。 RES /佈局/ ripple_animation.xml

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_marginLeft="6dp" 
    android:layout_marginRight="6dp" 
    android:layout_marginBottom="8dp" 
    android:layout_marginTop="8dp" 
    android:paddingBottom="30dp" 
    app:cardBackgroundColor="#e7e7e7" 
    android:id="@+id/cardview" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:background="@drawable/ripple_effect"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_marginRight="16dp" 
     android:layout_marginLeft="16dp" 
     android:paddingBottom="5dp"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Pending" 
       android:layout_weight="1" 
       android:layout_marginBottom="2dp" 
       android:textSize="25dp" 
       android:textColor="#d11010" 
       android:id="@+id/txtpending" /> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Assigned to:" 
       android:layout_marginLeft="20dp" 
       android:textColor="#ff000000" 
       android:id="@+id/txtassigned" 
       android:layout_weight="1"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:paddingBottom="5dp" 
      android:paddingTop="5dp" 
      android:layout_marginLeft="16dp"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="Student Name" 
       android:id="@+id/txtstudentname"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="Student Number" 
       android:id="@+id/txtstudentnumber"/> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="Parent Name" 
       android:id="@+id/txtparentname"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="Parent Number" 
       android:id="@+id/txtparentnumber"/> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:text="Transfer Status" 
       android:id="@+id/txttransfer"/> 

     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 
    </android.support.v7.widget.CardView> 

現在,連鎖反應是紅色。