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_dark
và colored_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.
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
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 –
Tôi đặt tint_menu_item.xml ở đâu? – rraallvv