图标未显示在操作栏上

时间:2014-03-30 07:53:28

标签: java android xml

我正在阅读谷歌的教程,无法弄清楚如何在行动栏上放置搜索图标。

这是main_activity_actions.xml代码: -

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <!-- Search, should appear as action button -->
   <item android:id="@+id/action_search"
         android:orderInCategory="100"
         android:icon="@drawable/ic_action_search"
         android:title="@string/action_search"
         android:showAsAction="always" />


   <!-- Settings, should always be in the overflow -->
   <item android:id="@+id/action_settings"
         android:title="@string/action_settings"
         android:showAsAction="never" />
</menu>

这是MainActivity.java: -

public class MainActivity extends ActionBarActivity
{
   public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

      getSupportActionBar().setDisplayHomeAsUpEnabled(true);

      if (savedInstanceState == null)
      {
         getSupportFragmentManager().beginTransaction()
               .add(R.id.container, new PlaceholderFragment()).commit();
      }
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu)
   {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_activity_actions, menu);
      return true;
   }

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

   public void openSearch()
   {

   }

   public void openSettings()
   {

   }

}

Manifest.xml:

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />

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

</manifest>

我已经看到了很少的答案并发布了堆栈溢出并尝试了它们仍然无效 当我尝试运行它时,搜索图标永远不会显示出来 有人可以建议做什么吗?

1 个答案:

答案 0 :(得分:2)

在activity_action.xml

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


     <!-- Search, should appear as action button -->
   <item android:id="@+id/action_search"
         android:orderInCategory="100"
         android:icon="@drawable/ic_action_search"
         android:title="@string/action_search"
        YOUR_PROJECT_NAME:showAsAction="always" />