来自ImageView的自定义Android菜单

时间:2013-08-09 13:26:30

标签: android eclipse menu

我希望我的Android应用能够显示类似菜单按钮的内容,但菜单会从图像视图中弹出。

this is an image. please upgrade your browser past DOS

2 个答案:

答案 0 :(得分:3)

您可以使用PopupWindow:

活动类:

公共类MainActivity扩展了Activity {

ImageView mOptionsImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mOptionsImage = (ImageView) findViewById(R.id.imageView1);
    mOptionsImage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popupWindow = new PopupWindow(popupView,
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            popupWindow.showAsDropDown(mButton, 0, v.getHeight());

            ListView list = (ListView) findViewById(R.id.list);

            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // SOME YOUR CODE
                    popupWindow.dismiss();
                }
            });
        }
    });
}

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="34dp"
    android:text="Button" />
</RelativeLayout>

和popupWindow布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView> </LinearLayout>

请注意:在您的列表为空之前,您将看不到任何结果,在测试此代码之前添加一些按钮,文本视图或填充列表与项目。;)

答案 1 :(得分:1)

ImageView imV = (ImageView) findViewById(R.id.imV); // if declared in XML
imV.setOnClickListener(imVListener);

View.OnClickListener imVListener = new View.OnClickListener() {
    public void onClick(View v) {
//you can use QuickAction menu to pop up
}
};

link到QuickAction菜单。