2012-10-05 30 views
11

Tôi muốn biết cách tạo kiểu cho cửa sổ bật lên thả xuống của Android 4.0 SearchView?Phong cách Android SearchView Trình đơn thả xuống

Tôi đang sử dụng Theme.Sherlock.Light.DarkActionBar, nhưng tôi không biết làm thế nào để tạo kiểu thả xuống tìm kiếm vào một nền trắng và một văn bản màu đen?

Trả lời

8

Vì một số lý do chúng sử dụng "searchAutoCompleteTextView" cũng không hoạt động đối với tôi. Vì vậy, tôi giải quyết nó bằng cách sử dụng đoạn mã sau khi thiết lập SearchView tôi:

Lưu ý: Đây là tất cả được thực hiện với sự hỗ trợ v7 android/thư viện AppCompat

public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.search_menu, menu); 

    MenuItem searchItem = menu.findItem(R.id.action_search); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 

    // Theme the SearchView's AutoCompleteTextView drop down. For some reason this wasn't working in styles.xml 
    SearchAutoComplete autoCompleteTextView = (SearchAutoComplete) searchView.findViewById(R.id.search_src_text); 

    if (autoCompleteTextView != null) { 
     autoCompleteTextView.setDropDownBackgroundResource(R.drawable.abc_search_dropdown_light); 
    } 
} 

Có hai tìm kiếm thả xuống nguồn lực được cung cấp bởi khả năng tương thích thư viện, họ

  • R.drawable.abc_search_dropdown_light (Light nền)
  • R.drawable.abc_search_dropdown_dark (nền tối)
+1

Điều đó hoạt động tốt ngoại trừ màu văn bản không đúng nhưng tôi sẽ xem thêm – Benoit

+0

@Benoit Đây 'một ví dụ về cách thay đổi màu văn bản - http://stackoverflow.com/a/ 14364222/1299623 – jimmithy

+0

R.id.search_src_text là gì? –

2

Như vậy là nhiều bước làm như vậy

Trước hết, bạn cần phải tạo ra một tùy chỉnh drawable với bốn tiểu bang, bạn có thể tham khảo {} ABSLibrary /res/drawable/abs__list_selector_holo_dark.xml. Nó sẽ là một cái gì đó như thế này:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_window_focused="false" android:drawable="@android:color/transparent" /> 

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> 
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/abs__list_selector_disabled_holo_dark" /> 
<item android:state_focused="true" android:state_enabled="false"        android:drawable="@drawable/abs__list_selector_disabled_holo_dark" /> 
<item android:state_focused="true"        android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_dark" /> 
<item android:state_focused="false"        android:state_pressed="true" android:drawable="@drawable/abs__list_selector_background_transition_holo_dark" /> 
<item android:state_focused="true"                android:drawable="@drawable/abs__list_focused_holo" /> 

Save the drawable tùy chỉnh trên (.xml) vào res dự án của riêng bạn/drawable. Chỉnh sửa kiểu cho phù hợp bằng cách tham khảo mẫu ở trên. Xin lưu ý rằng phong cách có thể được lồng sâu, chỉ cần kiên nhẫn nhìn xuống cây.

Sau đó tạo (hoặc đưa vào chủ đề tùy chỉnh hiện) một chủ đề tùy chỉnh với những điều sau đây, nó phải được lưu lại dưới dạng res/values ​​/ styles.xml:

<style name="Theme.MyCustomTheme" parent="Theme.Sherlock.Light.DarkActionBar"> 
<item name="searchAutoCompleteTextView">@style/MySearchAutoCompleteTextView</item></style> 

<style name="MySearchAutoCompleteTextView" parent="Sherlock.__Widget.SearchAutoCompleteTextView"> 
<item name="android:dropDownSelector">@drawable/myCustomDrawable_DropDown</item> 
<item name="android:popupBackground">@drawable/myCustomDrawable_popupBackground</item></style> 

Xin lưu ý rằng "myCustomDrawable_DropDown" và " myCustomDrawable_popupBackground "phải là tên của đối tượng vẽ tùy chỉnh mà bạn vừa tạo.

Bạn chỉ cần biết rằng "android: dropDownSelector" và/hoặc "android: popupBackground" là những người chịu trách nhiệm về việc theming của hộp bật lên tự động hoàn tất.

Cuối cùng, áp dụng chủ đề trong Tệp kê khai của bạn!

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/Theme.MyCustomTheme" > ... 
+0

Cảm ơn bạn đã trả lời, tôi đã có một chủ đề tùy chỉnh và tôi đã thiếu "searchAutoCompleteTextView" nhưng ngay cả với mục đó tôi dường như không thể thay đổi màu nền. Có thể cái gì đó đang đè bẹp nó. Tôi sẽ phải đào sâu hơn – Benoit

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