首先添加的依赖为 compile 'com.example.zhouwei.library:library:1.0.0'
除此之外还有popupwindow 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menu_pop_bg"
android:padding="10dp"
>
<LinearLayout
android:id="@+id/menu1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@mipmap/mersj2x"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="我的商家"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/menu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="10dp"
>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@mipmap/mersq2x"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:text="申请"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
然后只需要在点击实现中添加一个自定义的方法
//点击事件中自定义的方法 我这里是showPopMenu(); 设置成全全局就行
private void showPopMenu() {
View contentView = LayoutInflater.from(this).inflate(R.layout.pop_menu,null);
//处理popWindow 显示内容,这里面也嵌套了一个方法是显示内容的
handleLogic(contentView);
//创建并显示popWindow
mCustomPopWindow= new CustomPopWindow.PopupWindowBuilder(this)
.setView(contentView)
.create()
//backImgjiaTitletree 这个是我做点击事件的空间,点击后出现PopWindow布局效果
.showAsDropDown(backImgjiaTitletree,0,20);
}
//这个方法嵌套在showPopMenu()中,
handleLogic();
private void handleLogic(View contentView) {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mCustomPopWindow!=null){
mCustomPopWindow.dissmiss();
}
String showContent = "";
switch (v.getId()){
case R.id.menu1:
showContent = "点击 Item菜单1";
break;
case R.id.menu2:
showContent = "点击 Item菜单2";
break;
}
Toast.makeText(MerchantActivity.this,showContent,Toast.LENGTH_SHORT).show();
}
};
contentView.findViewById(R.id.menu1).setOnClickListener(listener);
contentView.findViewById(R.id.menu2).setOnClickListener(listener);
}
按照我的方法做就ok了,这个是参考 https://github.com/pinguo-zhouwei/CustomPopwindow 的链接可以看看各种效果已经显示出来了