如何制作像“分享菜单"这样的精美透明菜单?在 Android 画廊? [英] How to make a fancy transparent menu like the "share menu" in Android gallery?

查看:21
本文介绍了如何制作像“分享菜单"这样的精美透明菜单?在 Android 画廊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 2.2.2 设备上,图库看起来非常好.我想在我自己的应用程序中做的是按下一个按钮,然后显示一个如下所示的菜单:

On my Android 2.2.2 device the gallery looks really nice. What I would love to do in my own app is to press a button and then show a menu that looks like this:

这是使用任何标准的 Android 主题/样式吗?有人知道有这样一个菜单的示例代码吗?

Is this using any standard Android themes / styles? Does anybody know of an example code to have a menu like this?

我发现可以用一个对话框来模仿这个菜单.为了简化事情,我在这个例子中没有使用 ListView,只是对话框的一个 TextView 条目:

I figured out that one could mimic this menu with a Dialog. To simplify things I'm not using a ListView in this example, just a single TextView entry for the dialog:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              android:padding="10dp"
              />
</LinearLayout>

按下按钮时显示对话框:

Showing the dialog when I press a button:

                Dialog dialog = new Dialog(MyActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

                dialog.setContentView(R.layout.custom_dialog);

                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Test option 1");

                WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
                WMLP.gravity = (Gravity.BOTTOM | Gravity.LEFT);
                WMLP.x = 0;
                WMLP.y = 0;
                dialog.getWindow().setAttributes(WMLP);

                dialog.show();

这会创建一个靠近图片中菜单的对话框.但是,我仍然有两个问题:

This creates a dialog that comes close to the menu in the picture. I still have two problems, however:

1) 如何在对话框底部绘制这个小三角形,就像上图那样?

1) How can I draw this little triangle at the bottom of the dialog, like it is done in the picture above?

2) 应该打开对话框的按钮水平放置在底部按钮栏的中间.所以当我按下它时,对话框应该显示在那个按钮的正上方.我想做的是:

2) The button that is supposed to open the dialog is positioned horizontally in the middle of the bottom button bar. So when I press it, the dialog should be displayed right above that button. What I would like to do is this:

WMLP.x = middleButton.getLeft() + (middleButton.getWidth() / 2) - dialog.getWindow().getDecorView().getPaddingLeft() - (WMLP.width / 2);

问题是,WMLP.width 是 -2.我想原因是布局宽度设置为wrap_content"(即 -2),目前尚不清楚实际宽度.那么,如何确定对话框的宽度,以便将其与另一个视图同心放置?

The problem is, that WMLP.width is -2. I guess the reason is that the layout width is set to "wrap_content" (which is -2) and the actual width is not known at this moment. So, how can I determine the width of the dialog so that I can position it concentrical over another view?

更新:我终于找到了一个很好的对话来源:http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/

Update: I finally found a great source for a dialog like this: http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/

这正是我想要的,我现在正在我的应用中使用它.

It's exactly what I wanted and I'm using it in my app now.

推荐答案

你可以通过在当前位置添加一个 PopupWindow 来实现,为了绘制这个很酷的菜单,你需要有这个背景的图像和小箭头也可以画.

You can do this by adding a PopupWindow at the current location, for draw this cool menu you are going to need to have the image of this background and the litle arrow to draw too.

此外,您应该阅读有关九路径可绘制的内容来完成此操作.

Also you should read about the Nine-Path Drawable to get this done.

PS:我已经完成了,但有另一个图像.

PS: I already have this done, but with another image.

这篇关于如何制作像“分享菜单"这样的精美透明菜单?在 Android 画廊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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