您正在輸入的內容需要DLL namespace。用XML佈局替換[yourapp]
與您的應用程序名稱。爲SearchView屬性使用此名稱空間。如果您使用支持庫中的視圖,則此方法是必需的。順便說一句。你必須這樣做menu for AppCompat action bar。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:[yourapp]="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SearchView
android:id="@+id/fragment_address_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/global_bg_front"
android:textSize="@dimen/global_text_medium"
android:textColor="@color/global_text_primary"
[yourapp]:queryHint="@string/fragment_address_search_hint"
[yourapp]:iconifiedByDefault="false" />
</LinearLayout>
您也可以以編程方式設置您的SearchView。請參閱我的template on GitHub。
private void setupSearchView(SearchView searchView)
{
// search hint
searchView.setQueryHint(getString(R.string.fragment_address_search_hint));
// background
View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
searchPlate.setBackgroundResource(R.drawable.searchview_bg);
// icon
ImageView searchIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);
searchIcon.setImageResource(R.drawable.searchview_icon);
// clear button
ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setImageResource(R.drawable.searchview_clear);
// text color
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchText.setTextColor(getResources().getColor(R.color.global_text_primary));
searchText.setHintTextColor(getResources().getColor(R.color.global_text_secondary));
}
謝謝,我知道怎麼說工作了AppCompact但不知道它是爲支持庫 – forcewill
你能幫助我,如何使searchIcon消失,提示前不要使用同一空間? – AndrewS
AndrewS:嘗試使用AutoCompleteTextView而不是SearchView。 – petrnohejl