2013-10-30 26 views
15

我有一個佈局文件下面搜索查看Android的支持庫搜索查看XML屬性queryHint和iconifiedByDefault不工作

<android.support.v7.widget.SearchView 
    android:id="@+id/search_view" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:queryHint="@string/search" 
    android:iconifiedByDefault="false" 

    /> 

然而,當我運行我的應用程序沒有任何跡象出現,搜索查看圖標化,在代碼中它的工作原理我問題是這是一個支持圖書館的錯誤?

回答

38

您正在輸入的內容需要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)); 
} 
+0

謝謝,我知道怎麼說工作了AppCompact但不知道它是爲支持庫 – forcewill

+1

你能幫助我,如何使searchIcon消失,提示前不要使用同一空間? – AndrewS

+0

AndrewS:嘗試使用AutoCompleteTextView而不是SearchView。 – petrnohejl