2010-08-17 30 views
34

Tôi đã theo dõi this tutorial để tạo danh sách trạng thái màu cho chế độ xem Android cụ thể. Tôi chỉ muốn nó làm nổi bật khi được nhấp để người dùng biết tại sao màn hình chỉ thay đổi.Tuyến tính AndroidThanh toán với tài nguyên màu: Tôi đang làm gì sai?

Khi xem là ra, tôi nhận được lỗi sau:

org.xmlpull.v1.XmlPullParserException: dòng tập tin XML Binary # 3: thẻ đòi hỏi một thuộc tính hoặc con tag 'drawable' xác định một drawable

My XML màu (trong res/màu/viewcolor.xml):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:color="#ff33ffff"/> <!-- pressed --> 
    <item android:color="#ff000000"/> <!-- default --> 
</selector> 

bố trí của tôi XML (trong res/layout/myview.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/myview" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="top" 
    android:background="@color/viewcolor"> 
    <!--crap in the layout--> 
</LinearLayout> 

Tôi đã bỏ lỡ điều gì?

+0

nếu ai đó muốn có được giải pháp đầy đủ, kiểm tra kho lưu trữ này: https://github.com/shamanland/AndroidLayoutSelector có tùy chỉnh có thể click/checkable '' 'LinearLayout''' như một' '' ToggleButton''' –

Trả lời

52

Tôi nhớ rằng tôi đã khắc phục lỗi này bằng cách sử dụng trạng thái có thể vẽ được thay vì màu trạng thái. Đối với một số lý do bố trí nền chỉ không làm việc với màu sắc nhà nước. Vì vậy, hãy thử tạo ra một drawful stateful (ví dụ danh sách các drawables hình dạng với màu sắc khác nhau) và sử dụng nó như là nền tảng.

res/drawable/pressed.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
    <solid android:color="#ff33ffff" /> 
</shape> 

res/drawable/normal.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" > 
    <solid android:color="#ff000000" /> 
</shape> 

res/drawable/background.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/pressed" /> 
    <item android:drawable="@drawable/normal" /> 
</selector> 

Sau đó, sử dụng background.xml drawable làm nền :)

+0

Điều đó hoạt động hoàn hảo. Thật lạ là màu sắc không hoạt động hoàn toàn. Cảm ơn! – iandisme

+14

Thay vì sử dụng hình dạng trong thuộc tính có thể vẽ của bạn, thuộc tính android: drawable chấp nhận tài nguyên màu (ví dụ: @ color/black). –

49

Thay vì sử dụng hình dạng trong hình vẽ, bạn có thể sử dụng thuộc tính android:drawable chấp nhận tài nguyên màu (ví dụ: @màu đen).

Layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/myview" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="top" 
    android:background="@drawable/myDrawable"> 
    <!-- other views in layout--> 
</LinearLayout> 

my_drawable.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- focused --> 
    <item android:state_focused="true" android:drawable="@color/YOUR_COLOR_HERE" /> 
    <!-- focused and pressed--> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/YOUR_COLOR_HERE" /> 
    <!-- pressed --> 
    <item android:state_pressed="true" android:drawable="@color/YOUR_COLOR_HERE" /> 
    <!-- default --> 
    <item android:drawable="@color/YOUR_COLOR_HERE" /> 
</selector> 

Trong my_drawable.xml bạn cần phải chắc chắn rằng màu sắc bạn chỉ định được quy định tại res/values/colors.xml, hoặc điều này sẽ không công việc.

Nếu bạn muốn sử dụng hình ảnh thay vì thay đổi màu từ tài nguyên màu thành tài nguyên có thể kéo. Ví dụ:

android:drawable="@color/YOUR_COLOR_HERE" 
android:drawable="@drawable/YOUR_IMAGE_HERE" 
+2

Điều này không hoàn toàn phù hợp với tôi. Tôi đã phải thay đổi android: drawable trong các mục chọn để android: màu sắc và sau đó nó làm việc tốt. – emmby

+0

Cảm ơn bạn rất nhiều vì điều này, Austyn. – metter

+0

@emmby: Tôi đã thêm giải thích về những gì bạn cần làm để sử dụng mã này "ngoài hộp". –

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