使用AppCompat 22.1.0deviseAppCompat SearchView不起作用

我为build议行定义了布局,但在将AppCompat库更新为22.1之后,styles.xml中定义的布局将被忽略。

这是我的styles.xml文件(简体):

<style name="Theme.Upp" parent="@style/MyApp"> <item name="searchViewStyle">@style/SearchViewStyleMyApp</item> </style> <style name="SearchViewStyleMyApp" parent="Widget.AppCompat.SearchView"> <!-- Background for the search query section (eg EditText) --> <!-- <item name="queryBackground">@android:color/black</item> --> <!-- Background for the actions section (eg 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">@layout/layout_suggestion_entry</item> </style> 

即使我尝试更改queryBackground它不起作用。

任何想法为什么?

Solutions Collecting From Web of "使用AppCompat 22.1.0deviseAppCompat SearchView不起作用"

在查看最新的图书馆资源之后,它就像是打算使SearchView匹配材料devise指南一样,因此默认样式没有下划线和提示图标。

要首先应用自己的样式,必须在styles.xml中定义SearchView样式,如下所示:

 <style name="SearchViewStyleMyApp" parent="Widget.AppCompat.SearchView"> <!-- Background for the search query section (eg EditText) --> <!-- <item name="queryBackground">@android:color/black</item> --> ... <!-- note that this is how you stye your hint icon --> <item name="searchHintIcon">@drawable/ab_icon_search</item> </style> 

然后你定义你的ActionBar /工具栏主题覆盖:

 <style name="MyThemeOverlay.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="searchViewStyle">@style/SearchViewStyleMyApp</item> </style> 

下一步是将ToolBar添加到你的布局文件,如下所示:

 <LinearLayout 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:orientation="vertical" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:theme="@style/MyThemeOverlay.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:elevation="4dp" /> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> 

最后一步是设置你的工具栏像一个ActionBar:

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); } } 

另外请注意,您的主题必须扩展Theme.AppCompat.NoActionBar

如果您不使用ToolbarsearchViewStyle覆盖的样式是searchViewStyle下的searchViewStyle

 <style name="AppTheme" parent="Theme.AppCompat.Light"> ... <item name="actionBarTheme">@style/AppActionBarTheme</item> </style> <style name="AppActionBarTheme" parent="ThemeOverlay.AppCompat.ActionBar"> <item name="searchViewStyle">@style/AppActionBarSearchView</item> </style> <style name="AppActionBarSearchView" parent="Widget.AppCompat.SearchView.ActionBar"> <item name="queryBackground">@drawable/your_query_background</item> <item name="submitBackground">@drawable/your_submit_background</item> <item name="searchHintIcon">@drawable/your_search_hint_icon</item> <item name="queryHint">your query hint</item> </style>