我想制作一个 Action Bar
像
this one
我尝试了 Android sdk 中的示例,但它太复杂了,这个操作栏有 8 个 .java
文件?
希望有人能给我提供更简单的方法和更简单的方法来做。因为我不认为我会为了使 Action Bar
工作而将所有这 8 个文件复制到我的项目中。
最佳答案
这是一种方法。
开始在你的 res/menu 文件夹中添加一个菜单布局 action_bar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_one"
android:icon="@drawable/icon1.png"
android:showAsAction="always"
android:title="One">
</item>
<item
android:id="@+id/action_two"
android:icon="@drawable/icon2.png"
android:showAsAction="always"
android:title="Two">
</item>
</menu>
在你的 Activity 中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.setTitle("your title");
// add the custom view to the action bar
//actionBar.setCustomView(R.layout.actionbar_view);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_one:
//put your business logic here
break;
case R.id.action_two:
//put your business logic here
break;
case android.R.id.home:
//put your business logic here
break;
default:
// Nothing to do here
}
return true;
}
关于android - 如何制作包含标题和 2 个按钮的操作栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15310620/