步骤归纳: |
1 . 在Res目录下新建文件夹 menu |
2 .右键menu目录--> new --> Menu resource file |
3 . 添加代码 |
<item |
android:id= "@+id/add_item" |
android:title= "Add" /> |
<item |
android:id= "@+id/remove_item" |
android:title= "Remove" /> |
4 .在activity中重写onCreateOptionsMenu()方法 |
ctrl+o 重写 |
代码: |
public boolean onCreateOptionsMenu(Menu menu) { |
getMenuInflater().inflate(R.menu.main,menu); |
return true ; |
} |
5 .在activity中重写onOptionsItemSelected |
示例代码 |
public boolean onOptionsItemSelected(MenuItem item) { |
switch (item.getItemId()) { |
case R.id.add_item: |
Toast.makeText( this , "add" , Toast.LENGTH_SHORT).show(); |
case R.id.remove_item: |
Toast.makeText( this , "remove" , Toast.LENGTH_SHORT).show(); |
} |
return true ; |
} |
by: 发表于:2017-09-26 11:45:14 顶(0) | 踩(0) 回复
??
回复评论