操作项图标未显示在操作栏中

时间:2014-09-16 15:47:02

标签: java android android-actionbar

我正在尝试向action item添加action bar,但未显示操作项的图标。 XML文件res / menu / actionbar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="test"
      android:showAsAction="always" />         

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

添加action bar

import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

@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.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbar, 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();

    switch(id)
    {
    case R.id.action_search:
        Context context = getApplicationContext();
        CharSequence text = "Action Button Pressed!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();
        return true;
    default: 
        return super.onOptionsItemSelected(item);

    }

}



}

我将minimum sdk中的manifest file设置为7

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

1 个答案:

答案 0 :(得分:1)

尝试使用菜单xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/action_search"
  android:icon="@drawable/ic_action_search"
  android:title="test"
  app:showAsAction="always" />         

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