android不同的版本引入的actionbar有差异,现总结如下
一、在support.v7包中
引入布局使用onCreateOptionsMenu方法中的参数:inflate
import android.support.v7.app.ActionBarActivity; public class ComboLineColumnChartActivity extends ActionBarActivity { // MENU @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.combo_line_column_chart, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_reset) { reset(); generateData(); return true; } if (id == R.id.action_add_line) { addLineToData(); return true; } ...... return super.onOptionsItemSelected(item); } }
布局文件:
在menu文件夹中
R.menu.combo_line_column_chart
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="lecho.lib.hellocharts.samples.ComboLineColumnChartActivity" > <item android:id="@+id/action_reset" android:title="Reset chart" app:showAsAction="never"/> <item android:id="@+id/action_add_line" android:title="Add line" app:showAsAction="never"/> <item android:id="@+id/action_toggle_lines" android:title="Toggle lines/scattered" app:showAsAction="never"/> </menu>
参考hellocharts-android/combo_line_column_chart.xml at master · lecho/hellocharts-android
https://github.com/lecho/hellocharts-android/blob/master/hellocharts-samples/res/menu/combo_line_column_chart.xml
效果图:
2.在activity中
引入布局要使用activity下的getMenuInflate方法
public class ArticleListActivity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.list, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
布局代码:
在menu文件夹中
R.menu.list
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MyActivity" > <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" android:showAsAction="never" /> </menu>
参考:
hellocharts-android/combo_line_column_chart.xml at master · lecho/hellocharts-android
https://github.com/lecho/hellocharts-android/blob/master/hellocharts-samples/res/menu/combo_line_column_chart.xml
效果图: