2015-03-03 80 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