标签:
1. 创建res/menu/main.xml文件
<!-- 菜单栏的设置 --> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <!-- 默认的 --> <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="always" ></item> <!-- 添加的按钮 --> <item android:id="@+id/action_add" app:showAsAction="always" android:title="添加" android:icon="@android:drawable/ic_input_add"></item> </menu>
2. 加载 android.support.v7.app.ActionBarActivity
public class MainActivity extends ActionBarActivity{ // 这里需要继承 ActionBarActivity private Button bt = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) this.findViewById(R.id.MyBtn); System.out.println("onCreate"); } @Override public boolean onCreateOptionsMenu(Menu menu) { //菜单组件创建 this.getMenuInflater().inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // 导航栏按钮的监听 int id = item.getItemId(); if (id == R.id.action_add) { // 根据id来判断 bt.setText("click me"); } return super.onOptionsItemSelected(item); } }
标签:
原文地址:http://www.cnblogs.com/shaoshao/p/5903307.html