安卓仿微信菜单栏PopWindow

先构建好xml布局文件 用于弹窗的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="5dp"
    android:layout_marginTop="20dp"
    android:background="@drawable/popmenu"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_popmenu_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="10dp">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@mipmap/add" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical"
            android:text="记一笔"
            android:textColor="@color/fffwhite"
            android:textSize="20sp" />
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0.7dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/fffwhite" />

    <LinearLayout
        android:id="@+id/ll_popmenu_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@mipmap/book" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical"
            android:text="账本切换"
            android:textColor="@color/fffwhite"
            android:textSize="20sp" />
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0.7dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/fffwhite" />

    <LinearLayout
        android:id="@+id/ll_popmenu_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:src="@mipmap/search" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:gravity="center_vertical"
            android:text="搜索账本"
            android:textColor="@color/fffwhite"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

新建TopPopWindow extends PopupWindow。

package views;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.chase.cn.money_of_my.R;

/**
 * Created by Chase on 2017/2/23.
 */

public class TopPopWindow extends PopupWindow {
    private View mView;
    private LinearLayout ll_popmenu_record,ll_popmenu_book,ll_popmenu_search;

     public  TopPopWindow(Activity paramActivity, View.OnClickListener paramOnClickListener,
                              int paramInt1, int paramInt2){
         mView = LayoutInflater.from(paramActivity).inflate(R.layout.popwindow_topright, null);
         ll_popmenu_record = (LinearLayout) mView.findViewById(R.id.ll_popmenu_record);
         ll_popmenu_book = (LinearLayout) mView.findViewById(R.id.ll_popmenu_book);
         ll_popmenu_search = (LinearLayout) mView.findViewById(R.id.ll_popmenu_search);
         if (paramOnClickListener != null){
             //设置点击监听
             ll_popmenu_record.setOnClickListener(paramOnClickListener);
             ll_popmenu_book.setOnClickListener(paramOnClickListener);
             ll_popmenu_search.setOnClickListener(paramOnClickListener);
             setContentView(mView);
             //设置宽度
             setWidth(paramInt1);
             //设置高度
             setHeight(paramInt2);
             //设置显示隐藏动画
             setAnimationStyle(R.style.AnimTools);
             //设置背景透明
             setBackgroundDrawable(new ColorDrawable(0));

         }
     }

    }

使用:在自己的MainActivity调用showTopRightPopMenu()。

/**
     * 显示右上角popup菜单
     */
    private void showTopRightPopMenu() {
        /**
         * 用屏幕宽高计算popwindow的宽高
         */
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        int popWidth = width / 2;
        int popHeight = height / 3;
        if (topPopWindow == null) {
            //(activity,onclicklistener,width,height)
            topPopWindow = new TopPopWindow(MainActivity.this, this, popWidth, popHeight);
            //监听窗口的焦点事件,点击窗口外面则取消显示
            topPopWindow.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus) {
                        topPopWindow.dismiss();
                    }
                }
            });
            //pop消失时 背景调至原来的亮度
            topPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    backgroundAlpaha(MainActivity.this, 1.0f);
                }
            });
        }
        //设置默认获取焦点
        topPopWindow.setFocusable(true);
        //以某个控件的x和y的偏移量位置开始显示窗口
        topPopWindow.showAsDropDown(bt_add, 0, 18);
        backgroundAlpaha(MainActivity.this, 0.5f);
        //如果窗口存在,则更新
        topPopWindow.update();
    }

    /**
     * 设置添加屏幕的背景透明度 1.popwindow弹窗用作背景变暗
     **/
    public void backgroundAlpaha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值