2011-11-09 32 views
6

Tôi đang cố thay đổi kiểu TextView dựa trên trạng thái của nó. styles.xml My chứa:Thay đổi kiểu TextView state_pressed/state_focused/state_selected

<style name="text_normal_ops"> 
    <item name="android:gravity">left</item> 
    <item name="android:textColor">@color/text_usual_color</item> 
    <item name="android:textStyle">bold</item> 
</style> 
<style name="text_normal_ops_pressed"> 
    <item name="android:gravity">left</item> 
    <item name="android:textColor">@color/text_pressed</item> 
    <item name="android:textStyle">bold</item> 
</style> 

chọn của tôi (text_ops.xml) được định nghĩa là:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" style="@style/text_normal_ops_pressed" /> 
    <item android:state_focused="true" style="@style/text_normal_ops_pressed" /> 
    <item android:state_selected="true" style="@style/text_normal_ops_pressed" /> 
    <item style="@style/text_normal_ops"/> 
</selector> 

Nhưng khi tôi áp dụng điều này để TextView của tôi (style="@drawable/text_ops") nó không hoạt động. Bất kỳ mẹo nào?
Cảm ơn

+0

đã bạn giữ styles.xml ur trong res-> thư mục giá trị ??? – Hanry

+0

Có, tôi có hanry. – trenpixster

Trả lời

9

Trong Android theo kiến ​​thức của tôi, chỉ có hai danh sách trạng thái 1. Danh sách tài nguyên trạng thái màu 2. StateListDrawable. Nếu bạn đang sử dụng phong cách trong nó thì vui lòng kiểm tra lại doc

Vui lòng kiểm tra bên dưới liên kết để biết thêm

  1. http://developer.android.com/guide/topics/resources/color-list-resource.html
  2. http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
+0

Cảm ơn Maneesh, tôi đoán đó là vấn đề. Bạn có biết về bất kỳ công việc nào không? – trenpixster

1

Sự cố là dòng style="@drawable/text_ops" thì đây phải là kiểu = "@ style/text_ops".

Tôi chưa thử sử dụng bộ chọn cho kiểu nhưng sẽ rất tuyệt nếu nó hoạt động theo cách đó.

+0

Vâng, tôi biết ý bạn là gì. Tôi cũng nghĩ rằng đó là loại kỳ lạ để đặt tập tin chọn __styles__ trong thư mục __drawables__, nhưng tôi không nghĩ rằng nó có thể bao gồm bộ chọn bên trong 'values ​​/ styles.xml'. – trenpixster

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