将图标添加到Android上的操作栏

时间:2015-04-20 10:00:45

标签: java android android-actionbar

我按照下一个教程:Adding Action Buttoms,我一步一步地做了,但是当我运行我的应用程序时没有显示任何图标。我正在调试,但一切似乎都正确,我有任何nullPointerException或类似的东西。

这是我的主要活动:

package com.training.training.appone;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MyActivity extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "          com.        training.example.appone.MESSAGE";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);


 }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.menu_my, menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activiy_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view){
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.txt_edit_message);
    String message = editText.getText().toString();
    // El primer parametro es el KeyName:
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}
}

我把这个XML:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Search, should appear as action button -->
        <item android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    android:showAsAction="ifRoom" />
        <!-- Settings, should always be in the overflow -->
        <item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:showAsAction="never" />
    </menu>

我在最后一行“android:showsAsAction =”never“中有一个错误,我在我的构建gradle中下载并引用了libray:compile'c​​om.android.support:appcompat-v7:22.0.0'我也是尝试将“android”更改为“res-auto”,但它表示项目必须有标题(他们有):

   <?xml version="1.0" encoding="utf-8"?>
   <menu xmlns:android="http://schemas.android.com/apk/res/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"
    android:showAsAction="ifRoom" />
  <!-- Settings, should always be in the overflow -->
  <item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:showAsAction="never" />
   </menu>

我的图书馆:

AppCompat

在这一点上,我不知道发生了什么。

4 个答案:

答案 0 :(得分:2)

在menu.xml中进行以下更改

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

<item
    ...
    app:showAsAction="never" />

</menu>

如果你想在actionBar上显示你的菜单,那么它应该是app:showAsAction="always"

答案 1 :(得分:2)

在您的XML文件中,请更改此行

app:showAsAction="always"

而不是

android:showAsAction="never"

答案 2 :(得分:1)

可能在使用AppCompat库时,您应该使用

app:showAsAction="never"

代替

android:showAsAction="never"

答案 3 :(得分:1)

如果你看一下教程中的xml架构,你有两个:

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"

请注意 xmlns:android xmlns:yourapp 是不同的。所以你需要做的就是:

<?xml version="1.0" encoding="utf-8"?>
<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" />

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

可以根据需要命名 android yourapp 标记。它们只是模式的命名参考。