向上按钮:"<"在searchwidget显示时不显示

时间:2015-01-19 15:34:19

标签: android search android-actionbar

当我在操作栏中按下搜索按钮时,如何显示向上箭头? 这就是我要的 enter image description here enter image description here 这是在我的活动中的oncreate:

    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

这是我的styles.xml:

<style name="Theme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:icon">@drawable/ic_launcher</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_launcher</item>
    <item name="homeAsUpIndicator">@drawable/ic_launcher</item>
</style>

这是我的menu.xml

<item android:id="@+id/searchGrid"
    android:title="@string/search_hint"
    android:icon="@drawable/ic_action_search"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

清单文件:

    <activity
        android:theme="@style/Theme"
        android:launchMode="singleTop"
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>

OncreateOptionsMenu方法:

    this.menu = menu;
    getMenuInflater().inflate(currentActionBar, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =(SearchView) menu.findItem(R.id.searchGrid).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);

    searchView.setInputType(InputType.TYPE_CLASS_NUMBER);

    return true;

Build.gradle依赖项:

    compile 'com.android.support:appcompat-v7:21.0.3'

非常感谢你们,如果你们能为我解决这个问题,我一直在寻找几个小时。

1 个答案:

答案 0 :(得分:0)

如何将应用图标设置为.xml文件的背景?然后,您可以在应用程序图标的顶部添加一个按钮,并根据搜索栏是否打开来附加不同的处理程序。当搜索处于活动状态时,只需将按钮的文本设置为&#39;&lt;&#39;字符。