如何在Android的菜单选项中使用文本实现图像 [英] How to implement image with text in Menu options in android

查看:34
本文介绍了如何在Android的菜单选项中使用文本实现图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现菜单项列表中包含图像和文本的自定义菜单选项,但是我没有得到它...所以请帮助我如何实现它...谢谢您

i am trying so hard to implement customized menu option with both image and text in menu item list but i did not get it...so please help me how to implement it...thank you in advance

这是我的menu.xml

here is my menu.xml

<menu 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"
tools:context="com.example.admin.sst.Signuplogin">

<item android:id="@+id/one" android:title="one"

    app:showAsAction="never"
    />

<item android:id="@+id/two" android:title="two"
    android:orderInCategory="100" app:showAsAction="never" />

<item android:id="@+id/three" android:title="three"
    android:orderInCategory="100" app:showAsAction="never" />
</menu>

推荐答案

您可以在任何单击的视图上使用 PopupMenu .

You can use PopupMenu on any view clicked.

完整演示:

MainActivity:

package pk.sohail.gallerytest.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;

import java.lang.reflect.Field;

import pk.sohail.gallerytest.R;

public class MainActivity extends Activity {

    Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupMenu popupMenu = new PopupMenu(context, view);
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub
                        switch (item.getItemId()) {
                            case R.id.one:
                                Toast.makeText(getApplicationContext(), "1",
                                        Toast.LENGTH_SHORT).show();
                                return true;
                            case R.id.two:
                                Toast.makeText(getApplicationContext(), "2",
                                        Toast.LENGTH_SHORT).show();
                                return true;
                            case R.id.three:
                                Toast.makeText(getApplicationContext(), "3",
                                        Toast.LENGTH_SHORT).show();
                                return true;
                        }
                        return false;
                    }
                });
                popupMenu.inflate(R.menu.main);
                // Force icons to show
                Object menuHelper;
                Class[] argTypes;

                try {
                    Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
                    fMenuHelper.setAccessible(true);
                    menuHelper = fMenuHelper.get(popupMenu);
                    argTypes = new Class[]{boolean.class};
                    menuHelper.getClass()
                            .getDeclaredMethod("setForceShowIcon", argTypes)
                            .invoke(menuHelper, true);
                } catch (Exception e) {
                    // Possible exceptions are NoSuchMethodError and
                    // NoSuchFieldError
                    //
                    // In either case, an exception indicates something is wrong
                    // with the reflection code, or the
                    // structure of the PopupMenu class or its dependencies has
                    // changed.
                    //
                    // These exceptions should never happen since we're shipping the
                    // AppCompat library in our own apk,
                    // but in the case that they do, we simply can't force icons to
                    // display, so log the error and
                    // show the menu normally.

                    Log.w("as", "error forcing menu icons to show", e);
                    popupMenu.show();
                    return;
                }

                popupMenu.show();
            }
        });

    }
}

activity_main.xml:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pop Menu"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

R.menu.main:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.admin.sst.Signuplogin">

    <item
        android:id="@+id/one"
        android:icon="@mipmap/ic_launcher"
        android:title="one"
        app:showAsAction="never" />

    <item
        android:id="@+id/two"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="two"
        app:showAsAction="never" />

    <item
        android:id="@+id/three"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="100"
        android:title="three"
        app:showAsAction="never" />
</menu>

这篇关于如何在Android的菜单选项中使用文本实现图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆