2015-03-03 24 views
1

我不知道如何使搜索對話框適合高度,看起來像動作欄(如在YouTube上)。你能告訴我怎麼做嗎? 這裏是我的Mainfest按鈕被點擊時如何設計Android的onSearchRequested();

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="info.at_soft.zapiskiinfo" > 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name"> 
     <intent-filter> 
      <category android:name="android.intent.category.LAUNCHER" /> 
      <action android:name="android.intent.action.MAIN" /> 
     </intent-filter> 
    </activity> 

    <activity android:name=".SearchableActivity" android:launchMode="singleTop" > 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
     <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/> 
    </activity> 

    <meta-data android:name="android.app.default_searchable" android:value=".SearchableActivity"/> 
</application> 

代碼:

private void openSearch(){ 
    onSearchRequested(); 
    Toast.makeText(this, "Test", Toast.LENGTH_LONG).show(); 
} 

而且這裏也是我styles.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

</style> 
</resources> 

請看看這個照片:http://imgur.com/zhXmUSe

如何使它看起來就像這樣:下面http://imgur.com/nlCY0ES

回答

1

呼叫在活動的OnCreate:

setHasOptionsMenu(true); 
getActivity().getActionBar().setCustomView(R.layout.yourSearchBarLayout); 
+0

那是與搜索插件使得它,因爲我想和搜索對話框,使之?如果這不是我的搜索欄佈局應該包含的內容?還應該在哪個OnCreate上添加它? MainActivity或SearchableActivity?對不起,但我是Android應用程序開發的新手,有點困惑。 – 2015-03-03 14:25:38

+0

是的,將SearchView添加到您爲setCustomView創建的佈局,並將上面的代碼添加到您希望顯示的活動中。 – 2015-03-03 14:45:30