【问题标题】:How to add multiple icons on action bar in android?如何在android的操作栏上添加多个图标?
【发布时间】:2019-04-19 18:49:19
【问题描述】:

我想在 Android 应用的操作栏上添加 2 或 3 个图标。我已经采取了空活动并添加了工具栏。我还将图标设置在左侧。现在我想在上面添加另外两个图标。但是我的项目目录结构中没有 Menu 文件夹。所以有人告诉我如何在适当的指导下做到这一切? 我的代码在这里:

我的活动档案

    public class ActionBarActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_bar);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.left_nav);
        getSupportActionBar().setTitle("");
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

我的 .xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:fitsSystemWindows="true"
    tools:context="firstapp.vaibhav.com.firstapp.ActionBarActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

我的项目目录结构截图

【问题讨论】:

    标签: android android-actionbar icons


    【解决方案1】:

    1. 在现有资源res 文件夹中创建一个menu 文件夹。 (例如.../res/menu

    2.menu 文件夹中创建一个main.xml 文件。 (例如.../res/menu/main.xml

    ma​​in.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/action_item_one"
            android:title="Camera"
            android:icon="@drawable/ic_menu_camera"
            app:showAsAction="always" />
    
        <item
            android:id="@+id/action_item_two"
            android:title="Send"
            android:icon="@drawable/ic_menu_send"
            app:showAsAction="always" />
    </menu>
    

    3. 在您的活动中,覆盖 onCreateOptionsMenu()onOptionsItemSelected() 以使用选项菜单。

    ActionBarActivity.java

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_camera) {
    
            // Do something
            return true;
        }
        if (id == R.id.action_send) {
    
            // Do something
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    输出

    希望对你有所帮助~

    【讨论】:

      【解决方案2】:

      使用这样的项目创建 menu.xml

      <?xml version="1.0" encoding="utf-8"?>
      <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="never" />-->
          <item
              android:id="@+id/action_refresh"
              android:orderInCategory="100"
              app:showAsAction="always"
              android:icon="@drawable/ic_action_autorenew"
              android:title="Search"/>
         <item
             android:id="@+id/action_search"
             android:orderInCategory="100"
             app:showAsAction="always"
             android:icon="@drawable/ic_action_search"
             android:title="Search"/>
      
      </menu>
      

      并在活动中使用它

      @Override
          public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            //  MenuInflater inflater1 = getActivity().getMenuInflater();
              inflater.inflate(R.menu.cartmenu, menu);
              return ;
          }
      

      【讨论】:

        【解决方案3】:

        在您的 res/menu/menu_main.xml 中:

        添加

            <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
            <item android:id="@+id/icon_id"
                android:visible="true"
                android:title="@string/icon_name"
                android:icon="@drawable/your_image"
                app:showAsAction="always">
            </item>
        </menu>
        

        在你的活动中:

         @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_main, menu);
            // return true so that the menu pop up is opened
            return true; 
        }
        

        要访问activity 中的菜单项,请添加:

         @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.your_item_id) {
                // your code
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        

        【讨论】:

          【解决方案4】:

          您可以使用在菜单资源文件中的项目中找到的 showAsAction 选项。

          1) 如果要添加弹出菜单,请写app:showAsAction="never"

          2) 如果您想将图标添加为操作(操作栏中有多个图标),请写app:showAsAction="always"

          【讨论】:

          • 谢谢,这是我缺少的部分!
          猜你喜欢
          • 2016-04-21
          • 1970-01-01
          • 2016-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-31
          • 1970-01-01
          • 2017-06-11
          相关资源
          最近更新 更多