工具栏searchbuild议主题

我正在尝试将searchbuild议更改为“轻量级主题”。 我正在使用appcompat-v7:22.2.0库,并阅读有关自定义search视图窗口小部件(android.support.v7.widget.SearchView)的新function 。

第一部分

工具栏

<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:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

主旋律

 <style name="Main.Theme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/red</item> <item name="colorPrimaryDark">@color/red_dark</item> <item name="searchViewStyle">@style/Main.Theme.SearchView</item> </style> 

SearchView主题

 <style name="Main.Theme.SearchView" parent="Widget.AppCompat.Light.SearchView"> <item name="voiceIcon">@mipmap/test_icon</item> </style> 

这样我就无法影响search视图。 为了testing它,我正在更改search视图中的语音图标,并且它不会从默认更改。

第二个尝试部分

第二次尝试是覆盖工具栏中的叠加主题:

叠加主题

 <style name="Main.Theme.Overlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="searchViewStyle">@style/Main.Theme.SearchView</item> </style> 

这样我有一些反馈,但我失去了“材料devise”,特别是我有“旧”的提示图标,它的下划线。 我的最终目标是改变searchbuild议行背景:

build议行布局

 <item name="suggestionRowLayout">@layout/my_custom_layout</item> 

我觉得我很远,要完成这个…你能帮我吗?

Solutions Collecting From Web of "工具栏searchbuild议主题"

build议是由系统创build的dynamic列表,而不是一个活动。 所以你不能使用轻量级的主题。

相反,您可以通过使用样式来自定义build议,让它感觉像一个轻的主题。

使用这个hint_row.xml

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" style="@style/suggestionsTheme" android:layout_width="match_parent" android:layout_height="48dp" android:gravity="center_vertical" android:text="demo" android:paddingLeft="15dp" android:paddingRight="15dp" /> 

并应用这种风格

 <style name="suggestionsTheme" parent="TextAppearance.AppCompat.Light.SearchResult.Subtitle" > <item name="android:background">@android:color/white</item> <item name="android:textColor">@android:color/black</item> 

现在我正在得到这个

轻的建议

你可以根据你的需要给更多的风格

这是我如何使用我自己的searchbuild议…

这是我的CountriesFragment文件,只是在build议中显示所有国家的名称。

这里的诀窍是我提供自定义的View to Suggestion列表来显示。

 public class CountriesFragment extends Fragment { private boolean mSearchCheck; private SimpleCursorAdapter mAdapter; public static final String TEXT_FRAGMENT = "TEXT_FRAGMENT"; public static final String CITY_NAME = "cityName"; private LayoutInflater mInflater; private ViewGroup mainContainer; private static final String[] COUNTRIES = { "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (US)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; public static CountriesFragment newInstance(String text) { CountriesFragment mFragment = new CountriesFragment(); Bundle mBundle = new Bundle(); mBundle.putString(TEXT_FRAGMENT, text); mFragment.setArguments(mBundle); return mFragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadHints(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_countries, container, false); mInflater = inflater; mainContainer = (ViewGroup) rootView.findViewById(R.id.container); //rootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu, menu); SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search)); searchView.setQueryHint(this.getString(R.string.search)); ((EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text)) .setHintTextColor(getResources().getColor(R.color.nliveo_white)); searchView.setSuggestionsAdapter(mAdapter); searchView.setOnQueryTextListener(onQuerySearchView); searchView.setOnSuggestionListener(onQuerySuggestion); menu.findItem(R.id.menu_add).setVisible(true); menu.findItem(R.id.menu_search).setVisible(true); mSearchCheck = false; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add: addCountry(); break; case R.id.menu_search: mSearchCheck = true; break; } return true; } private void addCountry() { final ViewGroup newView = (ViewGroup) mInflater.inflate(R.layout.country_row, mainContainer, false); final TextView countryName = (TextView) newView.findViewById(android.R.id.text1); countryName.setText(COUNTRIES[(int) (Math.random() * COUNTRIES.length)]); final Animation fadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in); final Animation fadeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out); fadeIn.setFillAfter(false); fadeOut.setFillAfter(false); final ImageButton closeButton = (ImageButton) newView.findViewById(R.id.delete_button); closeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { newView.startAnimation(fadeOut); new Handler().postDelayed(new Runnable() { @Override public void run() { mainContainer.removeView(newView); } }, fadeOut.getDuration()); } }); mainContainer.addView(newView, 0); newView.startAnimation(fadeIn); } private SearchView.OnQueryTextListener onQuerySearchView = new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { mSearchCheck = false; return false; } @Override public boolean onQueryTextChange(String query) { if (mSearchCheck) { // implement your search here giveSuggestions(query); } return false; } }; private void giveSuggestions(String query) { final MatrixCursor cursor = new MatrixCursor(new String[]{BaseColumns._ID, CITY_NAME}); for (int i = 0; i < COUNTRIES.length; i++) { if (COUNTRIES[i].toLowerCase().contains(query.toLowerCase())) cursor.addRow(new Object[]{i, COUNTRIES[i]}); } mAdapter.changeCursor(cursor); } private SearchView.OnSuggestionListener onQuerySuggestion = new SearchView.OnSuggestionListener() { @Override public boolean onSuggestionSelect(int position) { return false; } @Override public boolean onSuggestionClick(int position) { return false; } }; private void loadHints() { final String[] from = new String[]{CITY_NAME}; final int[] to = new int[]{android.R.id.text1}; mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.hint_row, null, from, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); } } 

这里是hint_row.xml

 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="48dp" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:background="@color/nliveo_green_colorPrimaryDark" android:textColor="@color/nliveo_white" android:paddingLeft="15dp" android:paddingRight="15dp" android:minHeight="?android:attr/listPreferredItemHeightSmall" android:elevation="3dp" android:alpha="0.7" /> 

简单的菜单文件有SearchView。 menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_search" app:actionViewClass="android.support.v7.widget.SearchView" android:icon="@mipmap/ic_search_white_24dp" app:showAsAction="ifRoom|collapseActionView" android:title="@string/search"/> <item android:id="@+id/menu_add" android:icon="@mipmap/ic_add_white_24dp" app:showAsAction="ifRoom" android:title="@string/add"/> </menu> 

我正在得到这个美丽的build议…

自定义提示