android - 如何制作包含标题和 2 个按钮的操作栏?

标签 android android-actionbar

我想制作一个 Action Barthis one
我尝试了 Android sdk 中的示例,但它太复杂了,这个操作栏有 8 个 .java 文件?


ActionBarCompat


希望有人能给我提供更简单的方法和更简单的方法来做。因为我不认为我会为了使 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/

相关文章:

android - 如何检测屏幕镜像

java - 使用 Android 开发工具 v. 23 更新 Eclipse

android - 使用 ADB 命令获取 IMEI 号 Android 12

选项卡上的 Android 抽屉导航

android - 如何禁用操作栏上 "up"按钮的翻转?

Android native 进程: stack corruption detected

java - 无法使用 Retrofit 和 PHP 作为后端从 android 中的服务器接收任何响应

android-activity - v7-21 ActionBarActivity 不在左侧显示应用程序主图标

java - ActionBar 导航列表 - 在多个 Activity 中使用相同的 ActionBar;在一处初始化

android - 如何更改 android 溢出按钮上弹出菜单的位置?