【问题标题】:How to style Android's onSearchRequested();如何为 Android 的 onSearchRequested() 设置样式;
【发布时间】:2015-03-03 11:46:46
【问题描述】:

我不知道如何使搜索对话框适合高度并看起来像操作栏(就像在 YouTube 上一样)。你能告诉我怎么做吗? 这是我的 Mainfest

<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

【问题讨论】:

    标签: android search android-activity styles


    【解决方案1】:

    在活动的 OnCreate 中调用如下:

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

    【讨论】:

    • 这是用搜索小部件制作的,因为我想用搜索对话框制作它吗?如果不是我的搜索栏布局应该包含什么?我还应该在哪个 OnCreate 上添加它? MainActivity 还是 SearchableActivity?抱歉,我是 android 应用程序开发的新手,有点困惑。
    • 是的,将 SearchView 添加到您为 setCustomView 创建的布局中,并将上述代码添加到您希望它显示的活动中。
    猜你喜欢
    • 2014-11-22
    • 2013-12-06
    • 2014-02-24
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多