Android 搜索图标未显示在操作栏上 [英] Android Search icon not showing up on Action Bar

查看:42
本文介绍了Android 搜索图标未显示在操作栏上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到问题时,我正在 Android 上学习本教程.如主题中所述,我的搜索图标未显示在操作栏上.我在这一步遇到了问题.http://developer.android.com/training/basics/actionbar/adding-buttons.html我想我已经做了所有相同的事情,但仍然没有出现.我见过其他人遇到类似问题,但他们的回答对我不起作用.

I was following this tutorial on Android when i came across a problem. As mentioned in topic my search icon isn't showing up on the action bar. I had problem with that step. http://developer.android.com/training/basics/actionbar/adding-buttons.html I think i have done everything the same but still nothing shows up. I've seen other people having similar problem but their answers didn't work for me.

最糟糕的是,与其他人不同,与搜索图标相关的问题没有出现,我的甚至没有出现在他们的溢出时.也许它可以引导某人了解我的问题.

The worst thing is unlike others people problems related to search icon not showing up, mine isn't even showing up on overflow while theirs is. Maybe it can guide someone to what's my issue.

main_activity_actions.xml 代码如下

main_activity_actions.xml code below

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <!-- Search, should appear as action button -->
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="ifRoom"/>
    <!-- Settings, should always be in the overflow -->
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

</menu>

main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    <!-- Search, should appear as action button -->
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/action_search"
        app:showAsAction="ifRoom"/>
    <!-- Settings, should always be in the overflow -->
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never"/>

</menu>

activity.main.xml

activity.main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/button_send"
        android:onClick="sendMessage" />

</LinearLayout>

MainActivity.java

MainActivity.java

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.pawel.androidapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here.
        switch(item.getItemId())
        {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private void openSearch() {
        Toast.makeText(this, "Search pressed", Toast.LENGTH_SHORT).show();
    }

    private void openSettings() {
        startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));       
    }

    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

安卓应用清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pawel.androidapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.pawel.myfirstapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.pawel.androidapp.MainActivity" />
        </activity>

    </application>

</manifest>

推荐答案

我遇到了同样的问题.您正在使用 minSdkVersion="11",这是定义 ActionBar API 的时候.(http://developer.android.com/guide/topics/ui/actionbar.html)

I had the same problem. You're using minSdkVersion="11" which is when the ActionBar APIs were defined. (http://developer.android.com/guide/topics/ui/actionbar.html)

我错的那一行是:app:showAsAction="ifRoom"

我看过说明支持库使用的开发教程,我的产品线看起来和你的一样.我把它改成这样:android:showAsAction="ifRoom"

I had looked at the development tutorials which are illustrating use of the Support Library and my line looked the same as yours. I changed it to this: android:showAsAction="ifRoom"

它奏效了.使用android"命名空间而不是app"命名空间.(https://developer.android.com/training/basics/actionbar/添加-buttons.html)

and it worked. Use the "android" namespace instead of the "app" namespace. (https://developer.android.com/training/basics/actionbar/adding-buttons.html)

这篇关于Android 搜索图标未显示在操作栏上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆