带图标的PopMenus [英] PopMenus with icons

查看:91
本文介绍了带图标的PopMenus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现与带有图标的PopupMenu 完全一样的布局单击自定义工具栏中的.因此,我已经获得了答案的帮助,但是我面临的问题是:单击没有弹出.

I want to achieve exactly like PopupMenu with icons On layout click which is in Custom Toolbar . So , i have taken the help of an answer but issue i am facing is : Nothing is poping out on click .

POPLayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/poplayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/home" />

        <TextView
            android:id="@+id/tvDistance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HOME"
            android:layout_marginLeft="20dp"
            android:paddingTop="5dp"
            android:gravity="center_vertical"
            android:textColor="@color/backgroud_user" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/ic_cloud_upload" />

        <TextView
            android:id="@+id/cloud"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CHECKIN"
            android:layout_marginLeft="20dp"
            android:paddingTop="5dp"
            android:gravity="center_vertical"
            android:textColor="@color/backgroud_user" />
    </LinearLayout>

</LinearLayout>

Toolbar.xml(由要单击的布局组成)

  <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar 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"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolbar_height"
        android:background="@color/toolbar_color"
        android:contentInsetLeft="0dp"
        android:contentInsetStart="0dp"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textview"
                android:text="My Assignments"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_marginLeft="90dp"
                android:layout_weight="0.5"
                android:textSize="20sp"
                />

            <LinearLayout(**layout to be clicked**)
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:background="#75aadb"
                android:id="@+id/accountinfo_layout"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp">
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="6dp"
                    android:src="@drawable/account"/>

            </LinearLayout>


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

Code.class

accountInfoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               if(point!=null){
                    int[] location = new int[2];
                    v.getLocationOnScreen(location);
                    point = new Point();
                    point.x = location[0];
                    point.y = location[1];
                    showPopup(getActivity(),point);
                }

//显示弹出窗口的方法.

// The method that displays the popup.

  private void showPopup(final Activity context, Point p) {
            int popupWidth = 200;
            int popupHeight = 150;

            // Inflate the popup_layout.xml
            LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.poplayout);
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.pop_window, viewGroup);

            // Creating the PopupWindow
            final PopupWindow popup = new PopupWindow(context);
            popup.setContentView(layout);
            popup.setWidth(popupWidth);
            popup.setHeight(popupHeight);
            popup.setFocusable(true);

            // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
            int OFFSET_X = 30;
            int OFFSET_Y = 30;

            // Clear the default translucent background
            popup.setBackgroundDrawable(new BitmapDrawable());

            // Displaying the popup at the specified location, + offsets.
            popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

        }

单击时无显示.请帮助我查找错误.

Nothing in showing on click . Please help me in finding Bug .

推荐答案

我认为只要删除if(point!= null)条件,它将起作用

I think just remove if(point!=null) condition it will work

accountInfoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                    int[] location = new int[2];
                    v.getLocationOnScreen(location);
                    point = new Point();
                    point.x = location[0];
                    point.y = location[1];
                    showPopup(getActivity(),point);
                }
}
}

这篇关于带图标的PopMenus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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