android - 跟进我的第一个应用程序 Android 培训时未显示操作

标签 android android-actionbar android-appcompat

我仍在关注 Android 培训。我遇到了操作栏菜单的问题,您能告诉我为什么我的操作栏没有显示吗? 这是下面的代码:

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloworld.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="com.example.helloworld.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.Helloworld.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.Helloworld.MyActivity" />
    </activity>

    </application>

</manifest>

我在可绘制对象中添加了图标

MainActivity.java

    package com.example.helloworld;
    import android.support.v7.app.ActionBarActivity;
    import android.support.v4.app.Fragment;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    public class MainActivity extends ActionBarActivity {

        public final static String EXTRA_MESSAGE = "com.example.Helloworld.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    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);
    }



     /* @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, menu);

            MenuInflater inflater = getMenuInflater();

            inflater.inflate(R.menu.main_activity_actions, menu);
            return super.onCreateOptionsMenu(menu);
        }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    public void openSearch() {
        //Do something here.
    }

    public void openSettings() {
        //Do something here.
    }
}

ma​​in_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="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"
          yourapp:showAsAction="ifRoom"  />

     <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />

</menu>

字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Helloworld</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="edit_message">Message</string>
    <string name="button_send">Send</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">My Message</string>
    <string name="action_search">Search</string>

</resources>

这是 style.xml .. 请注意,我没有修改其中的任何内容,教程也没有提到任何相关内容。

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

最佳答案

按如下方式更改菜单:

ma​​in_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="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"
      yourapp:showAsAction="always"  />

 <item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:showAsAction="never" />

</menu>
  1. yourapp:showAsAction="always" 用于当您需要操作始终显示在操作栏上时。

  2. yourapp:showAsAction="ifRoom" 用于当您需要将操作显示在操作栏上(只有有空间或地方可以添加其他操作时)。

  3. yourapp:showAsAction="never" 用于当您需要您的操作不需要显示在操作栏上而是覆盖在菜单中时。

    <

关于android - 跟进我的第一个应用程序 Android 培训时未显示操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29427939/

相关文章:

java - 安卓 "Cannot resolve symbol ' @id/myLabel"

java - TabPagerIndicator 与抽屉导航出错

android - 使用应用程序包时,<1% 的用户会收到 UnsatisfiedLinkError

java - 访问 ActionBar 时出现 nullPointerException

android - 从android中的 fragment 中的actionbar菜单项textview获取值(value)

android - 如何在使用 appcompat21 时隐藏抽屉导航指示器

android - 添加Maven仓库报错——android.support.v7.widget.AppcompatTextView无法实例化

android - 在颜色选择器中使用 appcompat 的 ?colorAccent 似乎不起作用

中国的 Android GPS 位置跟踪

android - 从 viewpager 向后滑动会导致应用程序崩溃