如何设置带有图标和文本的操作栏菜单?

时间:2016-09-09 05:48:56

标签: android

如何设置带有图标和文字的操作栏菜单?喜欢这个

Action bar menu

4 个答案:

答案 0 :(得分:1)

首先选择那样的菜单

  

菜单/ setting_menu.xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:checked="true"
        android:id="@+id/first"
        android:icon="@drawable/first"
        android:title="first"/>
    <item
        android:id="@+id/second"
        android:icon="@drawable/second"
        android:title="second"/>

 </menu>
  

<强> demo.java

import android.app.Activity;

import android.os.Bundle;

import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;

import android.view.View;

import android.view.Window;


import android.widget.ImageView;

import java.lang.reflect.Field;
import java.lang.reflect.Method;



public class demo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo);


        Toolbar tb = (Toolbar) findViewById(R.id.toolbar1);


        ImageView ivSetting=(ImageView)findViewById(R.id.ivSetting);
        ivSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopup(view);
            }
        });



    }




    public void showPopup(View v) {
        PopupMenu popup = new PopupMenu(demo.this, v);


        //  popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        // showPopup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.setting_menu, popup.getMenu());



        try {
            Class<?> classPopupMenu = Class.forName(popup
                    .getClass().getName());
            Field mPopup = classPopupMenu.getDeclaredField("mPopup");
            mPopup.setAccessible(true);
            Object menuPopupHelper = mPopup.get(popup);
            Class<?> classPopupHelper = Class.forName(menuPopupHelper
                    .getClass().getName());
            Method setForceIcons = classPopupHelper.getMethod(
                    "setForceShowIcon", boolean.class);
            setForceIcons.invoke(menuPopupHelper, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Menu m = popup.getMenu();
        for (int i = 0; i < m.size(); i++) {
            MenuItem mi = m.getItem(i);

            //for aapplying a font to subMenu ...
            SubMenu subMenu = mi.getSubMenu();
            if (subMenu != null && subMenu.size() > 0) {
                for (int j = 0; j < subMenu.size(); j++) {
                    MenuItem subMenuItem = subMenu.getItem(j);



                }
            }

            //the method we have create in activity


        }
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                String btnName = null;

                switch (itemId) {

                    case R.id.first: {
                        return true;
                    }


                    case R.id.second: {

                        return true;
                    }



                }
                return true;
            }
        });
        popup.show();
    }


}
  

<强> demo.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"

    android:layout_height="match_parent">


    <include layout="@layout/toolbar"
        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>


</RelativeLayout>
  

<强> toolbar.xml

   
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar1"

    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/cardview_shadow_end_color"

    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="@android:color/holo_red_dark">

        <ImageView
            android:id="@+id/ivSetting"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_action_menu_setting"

           android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"

            />
</RelativeLayout>

    </android.support.v7.widget.Toolbar>

enter image description here

答案 1 :(得分:0)

只需在menu.xml文件中使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

        <item
            android:checked="true"
            android:id="@+id/drawer_how"
            android:icon="@drawable/change_lock"
            android:title="Forgot"/>
        <item
            android:id="@+id/drawer_chngP"
            android:icon="@drawable/cookies"
            android:title="Change Password Type"/>

                .....        

</menu>

答案 2 :(得分:0)

由于我无法评论,但上述解决方案无法正常工作,您的xml可能如下所示

<item 
 android:id="@id/menu_item"
 android:title="text"
 android:icon="@drawable/drawable_resource_name"
 android:showAsAction="always|withText" > </item>

如果手机中有足够的空间,请始终| withText&#39;将永远有效,如果没有空间它只会放置图标,您可以尝试使用您的模拟器进行测试,并让我们知道它是否有效。

答案 3 :(得分:0)

我认为这是PopupWindow 参考:PopupMenu with icons 附:我可以在这里发布整个代码,但是如果引用链接中的问题已经存在,那又有什么意义呢。看看吧。