2014-12-16 15 views
43

Làm thế nào để xác định android:state_selected bên trong một xml RippleDrawableLàm thế nào để thiết lập state_selected trong gợn drawable

Tôi đã sau cho gợn drawable nhưng màu nền không hiển thị khi tôi đặt myView.setSelected(true);

<?xml version="1.0" encoding="utf-8"?> 
<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#DDDDDD" 
    > 

    <item android:id="@android:id/mask"> 
     <shape> 
      <solid 
       android:color="@color/black" /> 
     </shape> 
    </item> 


    <item android:state_selected="true"> 
     <shape> 
      <solid 
       android:color="#EEEEEE" /> 
     </shape> 
    </item> 


    <item> 
     <color android:color="#FFFFFF" /> 
    </item> 

</ripple> 

Trả lời

70

Tìm thấy câu trả lời , chỉ trong trường hợp người khác có cùng sự cố

<?xml version="1.0" encoding="utf-8"?> 
<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="#DDDDDD" 
    > 


    <item> 
    <selector> 
     <item android:state_selected="true"> 
      <color android:color="#EEEEEE" /> 
     </item> 

     <item android:state_activated="true"> 
      <color android:color="#EEEEEE" /> 
     </item> 

     <item> 
      <color android:color="#FFFFFF" /> 
     </item> 
    </selector> 
    </item> 


</ripple> 
+1

Bạn không cần phải thiết lập một lớp mặt nạ ở đây từ mặt nạ và nội dung của bạn có giới hạn giống hệt nhau và độ mờ đục. Loại bỏ mặt nạ sẽ cải thiện hiệu suất đáng kể. – alanv

+0

Cảm ơn @alanv. Đã xóa mặt nạ. Bạn đã đề cập đến giới hạn và độ mờ đục giống hệt nhau, opacity hoạt động như thế nào trong một mặt nạ? – Sohaib

+7

Vì một số lý do nếu bạn sử dụng màu trong suốt cho trạng thái bình thường của bộ chọn thì bạn nên sử dụng mặt nạ nếu không thì gợn sẽ không được vẽ. – vovahost

6

Kết hợp câu trả lời ở trên với các câu trả lời khác từ:

What should be the color of the Ripple, colorPrimary or colorAccent? (Material Design)

Cung cấp cho một hiệu ứng gợn đẹp mà cũng làm việc cho khi mục là trong trạng thái được chọn.

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?attr/colorControlHighlight"> 
<!-- Ripple mask - applied to all selector states --> 
<item android:id="@android:id/mask"> 
    <color android:color="#42ffffff" /> 
</item> 
<!-- Selected state of item --> 
<item> 
    <selector> 
     <item android:state_selected="true"> 
      <color android:color="?attr/colorAccent" /> 
     </item> 
    </selector> 
</item> 
</ripple> 

này đi trong thư mục drawable-v21 của bạn, cho các nền tảng khác mà bạn chỉ có thể tạo ra một selector trong đó sử dụng màu sắc giọng:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@color/accent" android:state_selected="true"/> 
    <item android:drawable="@color/accent" android:state_pressed="true"/> 
</selector> 
7

Để thêm vào @Sohaib 's câu trả lời:

@Alanv đúng là OP không cần mặt nạ. Nhưng nếu một trong số các quốc gia chọn của bạn là minh bạch và bạn cần một chiếc mặt nạ nó đi ở đây:

<?xml version="1.0" encoding="utf-8"?> 
<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@color/ripple_color"> 

    <!-- mask here... --> 
    <item android:id="@android:id/mask"> 
     <color android:color="@color/black"/> <!-- any color will do --> 
    </item> 

    <item> 
     <selector> 
      <!-- ... NOT here. --> 
      <item android:state_selected="true"> 
       <color android:color="@color/blue"/> 
      </item> 

      <item android:state_activated="true"> 
       <color android:color="@color/red"/> 
      </item> 

      <item> 
       <color android:color="@color/transparent"/> 
      </item> 
     </selector> 
    </item> 
</ripple> 

tôi ban đầu có mặt nạ bên trong chọn của tôi và: bùng nổ:

1

tôi muốn bắt chước các hành vi của các vật liệu hộp kiểm thiết kế nhưng tôi không thể nhận được chúng phải cho đến khi tôi đã sử dụng một ColorStateList như thế này:

Trong drawable-v21/bg_checkbox_ripple.xml

<?xml version="1.0" encoding="utf-8"?> 
<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@color/checked_accent_statelist" 
    android:radius="24dp"> 
</ripple> 

Trong màu/checked_acce nt_statelist.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="?colorControlHighlight" android:state_checked="false"/> 
    <item android:color="?android:textColorHighlight" android:state_checked="true"/> 
</selector> 

Thuộc tính "android:? textColorHighlight" là màu nhấn của bạn, nhưng với tính minh bạch phù hợp với sử dụng nó trong gợn sóng (Tôi nghĩ rằng đó là 26%).

Ngoài ra, bạn nên cung cấp một dự phòng cho API trước 21 thiết bị trong drawable/bg_checkbox_ripple.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_shortAnimTime"> 
    <item> 
     <shape android:innerRadius="24dp" android:shape="oval"> 
      <solid android:color="@color/checked_accent_statelist"/> 
     </shape> 
    </item> 
</selector> 
Các vấn đề liên quan