2014-12-02 24 views
7

Tôi có chế độ xem tìm kiếm với nhà cung cấp nội dung cho các đề xuất tùy chỉnh, được hiển thị trong menu thả xuống. Tuy nhiên, nền thả xuống thì tối trong khi văn bản màu đen nên nó không hiển thị. Chủ đề ứng dụng của tôi được kế thừa từ Theme.AppCompat.Light để mọi thứ khác trong ứng dụng của tôi là văn bản màu đen trên nền sáng.Thay đổi màu nền của menu thả xuống tự động hoàn thành tìm kiếm

enter image description here

Tôi muốn thay đổi nền của menu thả xuống để văn bản có thể đọc được nhưng tôi đã không tìm thấy bất kỳ cách nào làm như vậy. Gần nhất tôi nhận được đã làm theo các giải pháp ở đây: Style Android SearchView Drop down popup Nhưng khi trình đơn thả xuống xuất hiện, mọi thứ trông có vẻ lộn xộn.

enter image description here

Có giải pháp làm việc nào cho điều này không?

+0

Bạn đã thử đề nghị Chris Banes'? http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html. Tìm tiêu đề phần SearchView Widget. –

Trả lời

9

Đây là một khả năng.

Xác định bố cục hàng đề xuất truy vấn, ví dụ: R.layout.li_query_suggestion, trông như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    android:id="@android:id/text1" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:gravity="center_vertical" 
    android:minHeight="56dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:textAppearance="?android:attr/textAppearanceListItemSmall" 
    android:textColor="@android:color/black" 
    tools:text="Blah blah blah"/> 

ý rằng màu nền là màu trắng và màu chữ là màu đen. Bạn có thể, tất nhiên, thay đổi các giá trị này.

Sau đó, trong bộ điều hợp con trỏ, hãy chỉ định bố cục bạn đã tạo làm bố cục hàng. Ví dụ:

CursorAdapter suggestionAdapter = new SimpleCursorAdapter(
    getActivity(), 
    R.layout.li_query_suggestion, 
    null, 
    new String[]{"name"}, 
    new int[]{android.R.id.text1}, 
    0); 

này sẽ cung cấp cho bạn cái gì đó trông giống như sau: enter image description here

Just FYI, chủ đề hiện tại của tôi trông như thế này:

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar"> 
    <item name="colorPrimary">@color/light_blue_500</item> 
    <item name="colorPrimaryDark">@color/light_blue_700</item> 
    <item name="colorAccent">@color/pink_a200</item> 
    <item name="android:windowActionBarOverlay">true</item> 
    <item name="searchViewStyle">@style/CustomSearchView</item> 
</style> 
6

tôi nghĩ rằng bạn nên sử dụng

SearchView.SearchAutoComplete autoCompleteTextView = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); 
if (autoCompleteTextView != null) { 
    autoCompleteTextView.setDropDownBackgroundDrawable(getResources().getDrawable(R.drawable.abc_popup_background_mtrl_mult)); 
} 

điều này có thể đặt backgroun thả xuống SearchAutoComplete d Light

14

phong cách Áp dụng bạn có thể thay đổi màu nền thả xuống và màu sắc các mục văn bản và kích thước

<!-- ToolBar --> 
<style name="ToolBarStyle" parent="Theme.AppCompat"> 
    <item name="android:textColorPrimary">@android:color/white</item> 
    <item name="android:textColorSecondary">@android:color/white</item> 
    <item name="actionMenuTextColor">@android:color/white</item> 
    <item name="android:dropDownItemStyle">@style/myDropDownItemStyle</item> 
    <item name="android:dropDownListViewStyle">@style/myDropDownListViewStyle</item> 
</style> 


<style name="myDropDownItemStyle" parent="Widget.AppCompat.DropDownItem.Spinner"> 
    <item name="android:textColor">@color/secondary_text_default_material_light</item> 
    <item name="android:textSize">14dp</item> 
</style> 

<style name="myDropDownListViewStyle" parent="Widget.AppCompat.ListView.DropDown"> 
    <item name="android:background">#FFF</item> 
</style> 
Các vấn đề liên quan