如何使用材料设计Android实现此自定义弹出菜单?

如何使用材料设计Android实现此自定义弹出菜单?,android,menu,material-design,Android,Menu,Material Design,我想实现一个自定义的弹出菜单,比如Android中的项目和图片,但我不知道是什么组件用于此 在材料设计网站上,谷歌展示。所以我认为,有一个本地解决方案可以实现这一点 我试过了,但我找不到如何自定义此视图的布局。有一个名为PopupMenu的小部件,它基本上是一个锚定到特定视图的菜单。一个缺点是默认情况下它不显示图标 但是,您可以使用反射并调用setforceshowcon来显示它们。您需要的代码是: 由于PopupMenu锚定到特定视图,因此ActionBar项具有actionLayout属

我想实现一个自定义的弹出菜单,比如Android中的项目和图片,但我不知道是什么组件用于此

在材料设计网站上,谷歌展示。所以我认为,有一个本地解决方案可以实现这一点


我试过了,但我找不到如何自定义此视图的布局。

有一个名为
PopupMenu
的小部件,它基本上是一个锚定到特定视图的菜单。一个缺点是默认情况下它不显示图标

但是,您可以使用反射并调用
setforceshowcon
来显示它们。您需要的代码是:

  • 由于
    PopupMenu
    锚定到特定视图,因此
    ActionBar
    项具有
    actionLayout
    属性。该布局(
    action\u item.xml
    )可以简单到:

    <Button
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="?attr/actionButtonStyle"
        android:layout_gravity="center"
        android:text="Show popup"
        android:textStyle="bold"
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"/>
    
  • 您的
    弹出菜单.xml
    ,您将为
    弹出菜单

    <menu
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/item1"
            android:title="Item1"
            android:icon="@mipmap/ic_launcher"/>
    </menu>
    
请注意,要获得菜单中的多行文本,您还需要对弹出菜单项使用
操作布局。

您可以使用,提交自定义适配器,通过它可以控制
列表PopupWindow
中每一行的布局。对于普通的
PopupWindow
来说,您必须提供一个锚定视图,另外还必须在
ListPopupWindow
的实例上调用
setContentWidth
,该实例通过其内容的大小设置弹出窗口的宽度。这是您必须付出的一个小代价,但对于一个小数据集来说并不是什么大问题。我使用此实用程序方法检索行的最大宽度:

public int measureContentWidth(ListAdapter adapter) {
    int maxWidth = 0;
    int count = adapter.getCount();
    final int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    final int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
    View itemView = null;
    for (int i = 0; i < count; i++) {
        itemView = adapter.getView(i, itemView, this);
        itemView.measure(widthMeasureSpec, heightMeasureSpec);
        maxWidth = Math.max(maxWidth, itemView.getMeasuredWidth());
    }
    return maxWidth;
}
public int-measureContentWidth(列表适配器){
int maxWidth=0;
int count=adapter.getCount();
最终int-widthmasurespec=MeasureSpec.makeMeasureSpec(0,MeasureSpec.unspected);
最终整数高度测量等级=测量等级MakeMasureSpec(0,测量等级未指定);
View itemView=null;
for(int i=0;i
使用一个。 碎片的好处在于,您可以轻松地 (如果您不理解片段,我建议您先阅读)


如果您想完全控制弹出内容,请参见

此代码在我的应用程序中有效

试试这个:-

<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=".LocationDetailsActivity">


<item xmlns:tools="http://schemas.android.com/tools"
    android:icon="@android:drawable/ic_menu_mapmode"
    app:showAsAction="ifRoom"
    android:title="@string/title_of_menu"
    tools:context=".LocationDetailsActivity">
    //the menu list with icon
    <menu>
        <item
            android:id="@+id/action_map_type_normal"
            android:orderInCategory="100"
            android:icon="some_icon" //place your icon here
            android:title="Vacation spots" />
        <item
            android:id="@+id/action_map_type_satellite"
            android:orderInCategory="100"
            android:icon="some_icon" //place your icon here
            android:title="Friends and family" />
        <item
            android:id="@+id/action_map_type_hybrid"
            android:orderInCategory="100"
            android:icon="some_icon" //place your icon here
            android:title="Restaurants" />
    </menu>
</item>

//带有图标的菜单列表

您可以从这些不同的提供者处浏览本教程

它们都有很好的示例和源代码来帮助您


希望这对您有所帮助:)

我也有同样的问题。但最终我发现,在我自己的解决方案中,我正在与您共享我的代码。希望这对你有帮助

}

public PopupWindow popupWindowDogs()
{

    // initialize a pop up window type
    PopupWindow popupWindow = new PopupWindow(this);

    // the drop down list is a list view
    final ListView listView = new ListView(this);
    // set our adapter and pass our pop up window contents
    listView.setAdapter(dogsAdapter(popUpContents));
    // listView.setBackgroundColor(Color.DKGRAY);
    listView.setBackgroundResource(R.drawable.ss4);
    listView.setPadding(0, 0, 0, 10);
    listView.setDivider(null);
    try {

        listView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                View c = listView.getChildAt(0);
                String cc = listView.getChildAt(0).toString();
                int scrolly = -c.getTop() + listView.getFirstVisiblePosition() * c.getHeight();
                /*
                 * Toast.makeText(getApplicationContext(), scrolly + "", Toast.LENGTH_SHORT)
                 * .show();
                 */}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {

            }
        });
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.toString() + "", Toast.LENGTH_SHORT)
                .show();
    }
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                long arg3) {
            try {

                // TODO Auto-generated method stub
                Context mContext = v.getContext();
                Swipetouch mainActivity = ((Swipetouch) mContext);
                // add some animation when a list item was clicked
                Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(),
                        android.R.anim.fade_in);
                fadeInAnimation.setDuration(10);
                v.startAnimation(fadeInAnimation);
                // dismiss the pop up
                mainActivity.popupWindowDogs.dismiss();
                // get the text and set it as the button text
                String val = (String) arg0.getItemAtPosition(arg2);
                // Toast.makeText(mContext, val, Toast.LENGTH_SHORT).show();
                if (val.equals("Signup Now")) {
                    Intent ii = new Intent(getApplicationContext(), Registration.class);
                    startActivity(ii);
                    stopService(new Intent(Swipetouch.this, MyService.class));
                    stopService(new Intent(Swipetouch.this, MyService.class));
                } else if (val.equals("Login")) {
                    Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
                    startActivity(ii);
                    stopService(new Intent(Swipetouch.this, MyService.class));

                } else if (val.equals("Exit")) {
                    finish();
                    stopService(new Intent(Swipetouch.this, MyService.class));
                } else if (val.equals("Friends")) {
                    Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
                    startActivity(ii);
                } else if (val.equals("Exit")) {
                    stopService(new Intent(Swipetouch.this, MyService.class));
                    finish();
                }

            } catch (Exception e) {
                Toast.makeText(Swipetouch.this, e.toString(), Toast.LENGTH_SHORT).show();
            }
        }

    });
    // some other visual settings
    popupWindow.setFocusable(true);
    popupWindow.setWidth(250);
    // popupWindow.setHeight(300);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

    // set the list view as pop up window content
    // SET WALLPAPER IMAGE
    /*
     * popupWindow.setBackgroundDrawable(getWallpaper()); popupWindow.setHeight(300);
     */
    // layout.setBackgroundResource(R.drawable.sshadow);
    // layout.setBackgroundColor(Color.TRANSPARENT);
    // popupWindow.setContentView(layout);

    popupWindow.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));
    popupWindow.setContentView(listView);

    return popupWindow;
}
public-PopupWindow-popupWindowDogs()
{
//初始化弹出窗口类型
PopupWindow PopupWindow=新的PopupWindow(此);
//下拉列表是一个列表视图
最终ListView ListView=新ListView(此);
//设置适配器并传递弹出窗口内容
setAdapter(dogsAdapter(popUpContents));
//setBackgroundColor(Color.DKGRAY);
setBackgroundResource(R.drawable.ss4);
setPadding(0,0,0,10);
setDivider(null);
试一试{
setOnScrollListener(新的OnScrollListener(){
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
视图c=listView.getChildAt(0);
字符串cc=listView.getChildAt(0.toString();
int scrolly=-c.getTop()+listView.getFirstVisiblePosition()*c.getHeight();
/*
*Toast.makeText(getApplicationContext(),scrolly+“”,Toast.LENGTH\u SHORT)
*.show();
*/}
@凌驾
public void onScroll(AbsListView视图、int firstVisibleItem、int visibleItemCount、,
整数totalItemCount){
}
});
}捕获(例外e){
Toast.makeText(getApplicationContext(),e.toString()+“”,Toast.LENGTH\u SHORT)
.show();
}
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共链接(AdapterView arg0,视图v,内部arg2,
长arg3){
试一试{
//TODO自动生成的方法存根
Context mContext=v.getContext();
Swipetouch main活动=((Swipetouch)mContext);
//单击列表项时添加一些动画
动画fadeInAnimation=AnimationUtils.loadAnimation(v.getContext(),
android.R.anim.fade_in);
设定持续时间(10);
v、 startAnimation(fadeInAnimation);
//关闭弹出窗口
mainActivity.popupWindowDogs.Disclose();
//获取文本并将其设置为按钮文本
字符串val=(字符串)arg0.getItemAtPosition(arg2);
//Toast.makeText(mContext、val、Toast.LENGTH_SHORT).show();
如果(val.equals(“立即注册”)){
Intent ii=新的Intent(getApplicationContext(),Registration.class);
星触觉(ii);
stopService(新意图(swiptouch.this、MyService.class));
stopService(新意图(swiptouch.this、MyService.class));
}else if(val.equals(“登录”)){
Intent ii=新的Intent(getApplicationContext(),MyLoginActivity.class);
星触觉(ii);
stopService(新意图(swiptouch.this、MyService.class));
}else如果(val.equals
<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=".LocationDetailsActivity">


<item xmlns:tools="http://schemas.android.com/tools"
    android:icon="@android:drawable/ic_menu_mapmode"
    app:showAsAction="ifRoom"
    android:title="@string/title_of_menu"
    tools:context=".LocationDetailsActivity">
    //the menu list with icon
    <menu>
        <item
            android:id="@+id/action_map_type_normal"
            android:orderInCategory="100"
            android:icon="some_icon" //place your icon here
            android:title="Vacation spots" />
        <item
            android:id="@+id/action_map_type_satellite"
            android:orderInCategory="100"
            android:icon="some_icon" //place your icon here
            android:title="Friends and family" />
        <item
            android:id="@+id/action_map_type_hybrid"
            android:orderInCategory="100"
            android:icon="some_icon" //place your icon here
            android:title="Restaurants" />
    </menu>
</item>
popupWindowDogs = popupWindowDogs();
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            // popupWindowDogs.showAsDropDown(v, -5, 0);
            popupWindowDogs().showAtLocation(v, Gravity.CENTER, 0, 0);
        }
    });
    // Detect touched area
    detector = new SimpleGestureFilter(this, this);
public PopupWindow popupWindowDogs()
{

    // initialize a pop up window type
    PopupWindow popupWindow = new PopupWindow(this);

    // the drop down list is a list view
    final ListView listView = new ListView(this);
    // set our adapter and pass our pop up window contents
    listView.setAdapter(dogsAdapter(popUpContents));
    // listView.setBackgroundColor(Color.DKGRAY);
    listView.setBackgroundResource(R.drawable.ss4);
    listView.setPadding(0, 0, 0, 10);
    listView.setDivider(null);
    try {

        listView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                View c = listView.getChildAt(0);
                String cc = listView.getChildAt(0).toString();
                int scrolly = -c.getTop() + listView.getFirstVisiblePosition() * c.getHeight();
                /*
                 * Toast.makeText(getApplicationContext(), scrolly + "", Toast.LENGTH_SHORT)
                 * .show();
                 */}

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                    int totalItemCount) {

            }
        });
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.toString() + "", Toast.LENGTH_SHORT)
                .show();
    }
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                long arg3) {
            try {

                // TODO Auto-generated method stub
                Context mContext = v.getContext();
                Swipetouch mainActivity = ((Swipetouch) mContext);
                // add some animation when a list item was clicked
                Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(),
                        android.R.anim.fade_in);
                fadeInAnimation.setDuration(10);
                v.startAnimation(fadeInAnimation);
                // dismiss the pop up
                mainActivity.popupWindowDogs.dismiss();
                // get the text and set it as the button text
                String val = (String) arg0.getItemAtPosition(arg2);
                // Toast.makeText(mContext, val, Toast.LENGTH_SHORT).show();
                if (val.equals("Signup Now")) {
                    Intent ii = new Intent(getApplicationContext(), Registration.class);
                    startActivity(ii);
                    stopService(new Intent(Swipetouch.this, MyService.class));
                    stopService(new Intent(Swipetouch.this, MyService.class));
                } else if (val.equals("Login")) {
                    Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
                    startActivity(ii);
                    stopService(new Intent(Swipetouch.this, MyService.class));

                } else if (val.equals("Exit")) {
                    finish();
                    stopService(new Intent(Swipetouch.this, MyService.class));
                } else if (val.equals("Friends")) {
                    Intent ii = new Intent(getApplicationContext(), MyLoginActivity.class);
                    startActivity(ii);
                } else if (val.equals("Exit")) {
                    stopService(new Intent(Swipetouch.this, MyService.class));
                    finish();
                }

            } catch (Exception e) {
                Toast.makeText(Swipetouch.this, e.toString(), Toast.LENGTH_SHORT).show();
            }
        }

    });
    // some other visual settings
    popupWindow.setFocusable(true);
    popupWindow.setWidth(250);
    // popupWindow.setHeight(300);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

    // set the list view as pop up window content
    // SET WALLPAPER IMAGE
    /*
     * popupWindow.setBackgroundDrawable(getWallpaper()); popupWindow.setHeight(300);
     */
    // layout.setBackgroundResource(R.drawable.sshadow);
    // layout.setBackgroundColor(Color.TRANSPARENT);
    // popupWindow.setContentView(layout);

    popupWindow.setBackgroundDrawable(new ColorDrawable(
            android.graphics.Color.TRANSPARENT));
    popupWindow.setContentView(listView);

    return popupWindow;
}
  try {
                    java.lang.reflect.Field[] fields = popup.getClass().getDeclaredFields();
                    for (java.lang.reflect.Field field : fields) {
                        if ("mPopup".equals(field.getName())) {
                            field.setAccessible(true);
                            Object menuPopupHelper = field.get(popup);
                            Class<?> classPopupHelper = Class
                                    .forName(menuPopupHelper.getClass().getName());
                            Method setForceIcons = classPopupHelper
                                    .getMethod("setForceShowIcon", boolean.class);
                            setForceIcons.invoke(menuPopupHelper, true);
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
try {
  Field mFieldPopup=popupMenu.getClass().getDeclaredField("mPopup");
  mFieldPopup.setAccessible(true);
  MenuPopupHelper mPopup = (MenuPopupHelper) mFieldPopup.get(popupMenu);
  mPopup.setForceShowIcon(true);
} catch (Exception e) {}