2012-02-27 22 views
13

Tôi có nút có nền được xác định bằng xml. Tôi muốn tint nút dựa trên trạng thái hiện tại nó là trong - tức là - ép, tập trung, bình thường.Thay đổi tông màu có thể kéo trong bộ chọn xml

Đây là tệp xml của tôi bên dưới. Ngoài ra, colored_tint_darkcolored_tint của tôi là cả hai màu mờ mà tôi đang cố vẽ lên hình ảnh có thể vẽ mà tôi gọi từ thư mục tài nguyên. Đây là vấn đề. Khi giao diện người dùng tải lần đầu tiên, hình ảnh có màu thích hợp trên đó, nhưng sau khi được nhấn, trạng thái được nhấn không hiển thị bất kỳ màu nào, trạng thái bình thường sẽ không hiển thị bất kỳ màu nào.

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

<item android:state_pressed="true" android:drawable="@drawable/rounded_grayscale_pinstripe_button"> 
    <shape> 
     <gradient 
      android:endColor="@color/colored_tint" 
      android:startColor="@color/colored_tint" 
      android:angle="270" /> 
     <stroke 
      android:width="0dp" 
      android:color="@color/colored_tint" /> 
     <corners 
      android:radius="0dp" /> 
     <padding 
      android:left="10dp" 
      android:top="10dp" 
      android:right="10dp" 
      android:bottom="10dp" /> 
    </shape> 
</item> 

<item android:state_focused="true" android:drawable="@drawable/rounded_grayscale_pinstripe_button"> 
    <shape> 
     <gradient 
      android:endColor="@color/colored_tint" 
      android:startColor="@color/colored_tint" 
      android:angle="270" /> 
     <stroke 
      android:width="0dp" 
      android:color="@color/colored_tint" /> 
     <corners 
      android:radius="0dp" /> 
     <padding 
      android:left="10dp" 
      android:top="10dp" 
      android:right="10dp" 
      android:bottom="10dp" /> 
    </shape> 
</item> 

<item android:drawable="@drawable/rounded_grayscale_pinstripe_button">   
    <shape> 
     <gradient 
      android:endColor="@color/colored_tint_dark" 
      android:startColor="@color/colored_tint_dark" 
      android:angle="270" /> 
     <stroke 
      android:width="0dp" 
      android:color="@color/colored_tint_dark" /> 
     <corners 
      android:radius="0dp" /> 
     <padding 
      android:left="10dp" 
      android:top="10dp" 
      android:right="10dp" 
      android:bottom="10dp" /> 
    </shape> 
</item> 

Tôi biết rằng có những giải pháp này trong java, nhưng tôi đặc biệt tìm kiếm một giải pháp trong xml. Cảm ơn.

Trả lời

1

Bạn đã thử với một số selector?

Bạn có thể tìm thấy một số ví dụ HERE

12

Tạo một tint_menu_item.xml selector:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/white" android:state_pressed="true" /> 
    <item android:color="@color/white" android:state_activated="true" /> 
    <item android:color="@color/green" /> 
</selector> 

(Trong ví dụ của tôi, hình ảnh có màu trắng khi được chọn, và màu xanh lá cây khi không được chọn)

Sau đó, trong xml của bạn, bạn có thể thêm thuộc tính tint vào ImageView:

<ImageView 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:tint="@color/tint_menu_item" 
    android:src="@drawable/ic_menu_home" /> 

Bạn cũng có thể sử dụng bộ chọn này trên một TextView sử dụng Format attibute:

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="@color/tint_menu_item" /> 
+0

HI, tôi có vấn đề với màu selector trên Api 18 và thấp hơn [link] (http://stackoverflow.com/questions/38673196/crash -bộ chọn lọc-tăng-xem-với-vector-draw-tint-color-chọn) Bạn có đề xuất vấn đề có thể là gì không? – Alex

+0

android: thuộc tính màu không hoạt động trên tất cả các apis. Để giải quyết vấn đề này, bạn có thể tạo tệp .png với màu phù hợp trực tiếp và sau đó xóa thuộc tính android: tint từ ImageView –

+0

Tôi đặt tint_menu_item.xml ở đâu? – rraallvv

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