如何设置SearchView AppCompat v21的样式

时间:2015-06-10 01:16:58

标签: android xml

我的ActionBar中有一个SearchView,我想自定义它的样式,但我还没有找到它。

我在v21 \ styles.xml

中尝试了这个
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="searchViewStyle">@style/AppTheme.SearchView</item>
</style>

<style name="AppTheme.SearchView" parent="Widget.AppCompat.Light.SearchView">
    <item name="android:queryBackground">@color/white</item>
    <item name="android:submitBackground">@color/white</item>
</style>

这是我的menu.xml中的SeachView

<item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    appcompat:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom"/>

编辑:在xml中设置queryHint和iconifiedByDefault不起作用,但在活动中设置它。有谁知道为什么?这很奇怪......

2 个答案:

答案 0 :(得分:3)

正如Android Developers Blog所述:

值/的themes.xml:

<style name=”Theme.MyTheme” parent=”Theme.AppCompat”>
    <item name=”searchViewStyle”>@style/MySearchViewStyle</item>
</style>
<style name=”MySearchViewStyle” parent=”Widget.AppCompat.SearchView”>
    <!-- Background for the search query section (e.g. EditText) -->
    <item name="queryBackground">...</item>
    <!-- Background for the actions section (e.g. voice, submit) -->
    <item name="submitBackground">...</item>
    <!-- Close button icon -->
    <item name="closeIcon">...</item>
    <!-- Search button icon -->
    <item name="searchIcon">...</item>
    <!-- Go/commit button icon -->
    <item name="goIcon">...</item>
    <!-- Voice search button icon -->
    <item name="voiceIcon">...</item>
    <!-- Commit icon shown in the query suggestion row -->
    <item name="commitIcon">...</item>
    <!-- Layout for query suggestion rows -->
    <item name="suggestionRowLayout">...</item>
</style>

答案 1 :(得分:1)

// Configure the search products widget
SearchManager searchManager = (SearchManager)mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView)menu.findItem(R.id.action_search).getActionView();

if(searchView != null)
{
    // Assumes current activity is the searchable activity
    searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));

    // Do not iconify the widget, expend it by default
    searchView.setIconifiedByDefault(false);

    // Customize the search view
    searchView.setQueryHint(getResources().getString(R.string.search_hint));
    searchView.setMaxWidth(1000);
    searchView.setWeightSum(5.0f);

    View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
    EditText searchSourceText = (EditText)searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

    searchPlate.setBackgroundColor(Color.TRANSPARENT);
    searchPlate.setMinimumWidth(10000);
    searchSourceText.setTextColor(Color.WHITE);
    searchSourceText.setWidth(10000);

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String s)
        {
            Log.v("onQueryTextSubmit", String.format("Query of search: %s", s));

            Intent searchIntent = new Intent(mActivity, SearchProducts.class);
            searchIntent.putExtra(SearchProducts.QUERY, s);

            startActivity(searchIntent);

            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) { return false; }
    });

    searchView.setOnCloseListener(new SearchView.OnCloseListener()
    {
        @Override
        public boolean onClose()
        {
            Log.v("Main", "searchView::OnCloseListener");
            return false;
        }
    });
}