如何设置android.support.v7.widget.SearchView的样式

时间:2015-10-29 19:29:50

标签: android android-layout searchview

我想设置searchIcon的色调,光标的颜色,suggestionRow的布局(即大小,颜色,图标)。如何控制SearchView的外观及其建议下拉?

到目前为止,我有:

搜索查看

 <android.support.v7.widget.SearchView
    
    android:id="@+id/search_view"

        android:layout_width="match_parent"
    
    android:layout_height="wrap_content"
    
    android:background="@color/primary_light"
    
    >

searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint"
    android:searchSuggestAuthority="com.example.MySuggestionProvider"
    android:searchSuggestSelection=" ?" >
</searchable>

更新

所以我发现以下内容有所帮助,但我仍然无法在下拉列表中设置文本的大小

SearchView.SearchAutoComplete autoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);

2 个答案:

答案 0 :(得分:2)

我的Utils中有一个方法可以对SearchView进行一些样式更改。也许这可以帮到你。

我告诉你,这种方法不需要关心建议。仅适用于SearchView的某些风格点:

public static SearchView customizeWhiteSearchView(SearchView sv, Context context, String hintText){

    EditText searchEditText = (EditText) sv.findViewById(android.support.v7.appcompat.R.id.search_src_text);  
    searchEditText.setTextColor(Color.WHITE);  
    searchEditText.setHintTextColor(context.getResources().getColor(R.color.light_light_gray));  

    ImageView imgViewSearchView = (ImageView) sv.findViewById(android.support.v7.appcompat.R.id.search_button);
    imgViewSearchView.setImageDrawable(context.getResources().getDrawable(R.drawable.button_cerca));

    SpannableStringBuilder ssb = new SpannableStringBuilder("   ");
    if (hintText != null){ ssb.append(hintText); }
    else{ ssb.append("Mínim 3 caràcters..."); }

    Drawable searchIcon = context.getResources().getDrawable(R.drawable.button_cerca);
    int textSize = (int) (searchEditText.getTextSize() * 1.25);
    searchIcon.setBounds(0, 0, textSize, textSize);
    ssb.setSpan(new ImageSpan(searchIcon), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    searchEditText.setHint(ssb);

    ImageView close = (ImageView) sv.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
    close.setImageResource(R.drawable.button_close);

    return sv;
}

答案 1 :(得分:-1)

试试这个:

 <item
        android:id="@+id/action_search"
        android:orderInCategory="200"
        android:title="Search"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>