2012-02-26 39 views
13

Tôi muốn biết cách thực hiện, nếu có thể, để tùy chỉnh màu của menu thả xuống từ AutoCompleteTextView khi được chọn. Tôi có thể tùy chỉnh mọi thứ khác, nhưng không phải là màu đã chọn - nó vẫn giữ nguyên.Cách tùy chỉnh menu thả xuống AutoCompleteTextView trên Android

Trong Activity:

ArrayAdapter<String> adap = new ArrayAdapter<String>(this, R.layout.row, strings); 
autoNewBird = (AutoCompleteTextView)findViewById(R.id.autoCompleteBirdName); 
autoNewBird.setAdapter(adap); 

row.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/birdtext" 
android:padding="5dip" 
android:background="@drawable/custom_spinner" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
style="@style/spinner_item" 
android:gravity="center_vertical" 
android:layout_gravity="center_vertical" android:lines="1"/> 

và custom_spinner.xml drawable (trong thư mục drawable)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_window_focused="false" android:state_enabled="true" 
android:drawable="@drawable/listback" /> 
<item android:state_window_focused="false" android:state_enabled="false" 
    android:drawable="@drawable/listback" /> 
<item android:state_pressed="true" android:drawable="@drawable/threebythree" /> 
<item android:state_enabled="true" android:state_focused="true" 
android:drawable="@drawable/threebythree" />    
<item android:state_enabled="true" android:drawable="@drawable/listback" /> 
<item android:state_focused="true" android:drawable="@drawable/listback" /> 
<item android:drawable="@drawable/listback" /> 
</selector> 

này làm việc cho một thả xuống spinner , nhưng đối với một số AutoCompleteTextView, khi được chọn, nó sẽ không thay đổi màu sắc như trình đơn thả xuống.

Mọi trợ giúp hoặc trải nghiệm với điều này sẽ được đánh giá cao.

Trả lời

11

Tôi đoán tôi đã tìm ra, hoặc đúng hơn là những gì tôi đã làm sai. Trong một chủ đề tôi đặt này:

<item name="android:autoCompleteTextViewStyle">@style/custom_autocomplete</item> 

và phong cách là (hoặc một phần có liên quan):

<item name="android:dropDownSelector">@drawable/custom_spinner</item> 

và custom_spinner trên cao kia.

Hy vọng điều này có thể giúp ai đó.

+0

Đây là giải pháp đúng. Để biết rõ hơn, dòng thứ hai phải được định kiểu như sau: . Hy vọng rằng sẽ giúp :) – Sandra

+0

Bố cục nào bạn đang sử dụng cho bố cục? Tôi đang làm như thế này aAdapterAutoComplete = new ArrayAdapter (getActivity(). GetApplicationContext(), R.layout.auto_complete_text, gợi ý); \t \t \t \t \t autoComplete.setAdapter (aAdapterAutoComplete); – user1163234

5

Trong xml, cho AutoCompleteTextView

android:dropDownSelector="@drawable/some_drawable" 
+0

Chỉ điều này làm việc cho tôi. Thêm: @ drawable/custom_spinner vào lớp kiểu tự động hoàn thành của tôi không hoạt động vì không phải tất cả thuộc tính đều hoạt động trong tệp kiểu và thay vào đó phải được thêm vào định nghĩa tự động hoàn tất trong bố cục xml. – Mike6679

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