Why don't AppCompat v21 SearchView styles work

2019-08-07 20:06发布

问题:

I am trying to follow the "SearchView Widget" section in this blogpost to style my SearchView. I have added searchIcon, queryBackground and submitBackground values in my res/values-v21/styles.xml but the styles don't seem to apply:

I am posting the code of the SSCCE and also posting the zipped eclipse project here, in case somebody will like to try it.

SSCCE

res/values-v21/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- <item name="android:colorPrimary">@color/primaryColor</item>
        <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:colorAccent">@color/accentColor</item>-->

        <item name="searchViewStyle">@style/CustomSearchViewStyle</item>
    </style>

    <style name="CustomSearchViewStyle" parent="Widget.AppCompat.SearchView">
        <!-- Search button icon -->
        <item name="searchIcon">@drawable/ic_account_circle_black_24dp</item>
        <!-- Background for the search query section (e.g. EditText) -->
        <item name="queryBackground">@color/searchViewQueryBackground</item> <!-- Lime 100 -->
        <!-- Background for the actions section (e.g. voice, submit) -->
        <item name="submitBackground">@color/searchViewSubmitBackground</item> <!-- Brown 100 -->
    </style>
</resources>

res/values/styles.xml

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

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>

        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="colorAccent">@color/accentColor</item>
    </style>


    <style name="AppTheme" parent="AppBaseTheme">
    </style>


    <style name="CustomToolbarTheme" parent="AppBaseTheme">
        <item name="android:textColorPrimary">#FFFFFF</item>
        <item name="android:textColorSecondary">#FFA400</item>
    </style>

    <style name="CustomToolbarPopupTheme" parent="AppBaseTheme">
        <item name="android:background">#1976D2</item>
    </style>

</resources>

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primaryColor">#2196F3</color> <!-- Blue 500 -->
    <color name="primaryColorDark">#1976D2</color> <!-- Blue 700 -->
    <color name="accentColor">#C51162</color> <!-- Maroon Dark  -->

    <color name="searchViewQueryBackground">#F0F4C3</color><!-- Lime 100 -->
    <color name="searchViewSubmitBackground">#D7CCC8</color><!-- Brown 100 -->
</resources>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="practice_projects.material_design_google_now_like_searchbox_four.MainActivity" >

    <include android:id="@+id/mainActivity_toolBar"
        layout="@layout/app_bar" />

    <TextView
        android:layout_below="@id/mainActivity_toolBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

app_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/CustomToolbarTheme"
    app:popupTheme="@style/CustomToolbarPopupTheme" >

    <include android:id="@+id/appBar_searchBox"
        layout="@layout/search_box" />

</android.support.v7.widget.Toolbar>

search_box.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:hint="@string/searchHint"
    app:iconifiedByDefault="false" >


</android.support.v7.widget.SearchView>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.mainActivity_toolBar);

        setSupportActionBar(toolbar);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.primaryColorDark)); 
        }

        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources() 
                .getColor(R.color.primaryColor)));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }
}