繁体   English   中英

如何在android中的操作栏上添加多个图标?

[英]How to add multiple icons on action bar in android?

我想在Android应用中的动作栏上添加2或3个图标。 我已经完成了空活动并添加了工具栏。 我还在左侧设置了Icon。 现在我想在其上添加另外两个图标。 但是我的项目目录结构中没有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>

我的项目目录结构的屏幕截图

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

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

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/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);
}

OUTPUT

在此输入图像描述

希望这会有所帮助〜

用这样的项创建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 ;
    }

在您的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);
}

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

1)如果你想添加弹出菜单然后写app:showAsAction="never"

2)如果你想添加图标作为动作(动作栏中有多个图标),那么写app:showAsAction="always"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM