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

时间:2017-04-05 07:40:27

标签: android android-actionbar icons

我想在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>

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

4 个答案:

答案 0 :(得分:15)

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

2. main.xml文件夹中创建menu文件。 (例如.../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);
}

<强>输出

enter image description here

希望这会有所帮助〜

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

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

2)如果要将图标添加为操作(操作栏中的多个图标),请输入app:showAsAction="always"

相关问题