2014-12-11 17 views
36

Tôi cố gắng để sử dụngsử dụng một màu sắc tùy chỉnh cho nền nút khi sử dụng selectableItemBackground thuộc tính

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

để có được nút của tôi để làm hiệu ứng thích hợp cho mỗi phiên bản Android như loang vv

Nhưng điều này tạo ra một nút có màu xám khi tôi cần một màu khác.

Làm cách nào để ghi đè màu mặc định này?

+0

Bạn đã tìm thấy giải pháp cho điều này chưa? – rafid059

+0

Tương tự ở đây, cố gắng để điều này vào năm 2016. –

Trả lời

10

Nếu bạn đọc source code of Button.java thì bạn sẽ thấy rằng đó là lớp con của TextView.java. Tôi đã thực hiện giải pháp đơn giản cho vấn đề được đề cập.

<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> 

Trong mã:

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

Sẽ tốt hơn nhiều nếu ai đó có thể mở rộng các lớp TextView và thực hiện một nút tùy chỉnh với các tính năng trong câu hỏi.

Lưu ý: minsdk của tôi là 14. Ngoài ra, các hiệu ứng kẹo gợn làm việc tốt

-1

thay vì sử dụng android:? Attr/selectableItemBackground bạn có thể tạo một xml trong drawable trong thư mục với nội dung sau đây.

<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

điều này không làm "hiệu ứng thích hợp cho mỗi phiên bản Android như gợn sóng, v.v ..."như được yêu cầu trong câu hỏi –

-2

Sửa: đây là bây giờ có thể sử dụng AppCompat và backgroundTint

android:backgroundTint="@color/yourColor" 

giải pháp trước:

tôi đã cùng một vấn đề và kết thúc làm điều này lập trình:

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 dường như không thay đổi màu nền cho tôi. –

4

Chỉ cần di chuyển màu mong muốn của bạn ở bên ngoài/cấp độ gốc, ví dụ: "Màu @/Đỏ" là nút màu:

<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

Sử dụng phương pháp này, bạn có thể tùy chỉnh màu hiệu ứng gợn sóng. Trước tiên, bạn phải tạo một tệp xml trong thư mục tài nguyên có thể drawable của bạn. Tạo tệp ripple_effect.xml và thêm mã sau đây. Tôi sử dụng màu nền như màu đỏ.

res/drawable/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> 

Và thiết lập nền để trên tập tin tài nguyên drawable. Mã cuối cùng của hoạt động bố cục xml trông như thế này. res/layout/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> 

Bây giờ, hiệu ứng gợn là màu đỏ.

Các vấn đề liên quan