右上角菜单利用poponwindow(可自定义样式)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/androidkwd/article/details/52804394
/**
 * 右上角菜单
 */
menu_bt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showPopupWindow(v);
    }
});
private void showPopupWindow(View view) {

    // 一个自定义的布局,作为显示的内容
    View contentView = LayoutInflater.from(this).inflate(
            R.layout.menu_pop_item, null);
    // 设置按钮的点击事件
   
Button action_setup=(Button)contentView.findViewById(R.id.action_setup);
action_setup.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        fm.beginTransaction().replace(R.id.fragment, SetupFrafment.newInstance()).commit();
    }
});
final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return false; // 这里如果返回true的话,touch事件将被拦截 // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss } }); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 // 我觉得这里是API的一个bug popupWindow.setBackgroundDrawable(getResources().getDrawable( R.drawable.shape_popwindow)); // 设置好参数之后再show popupWindow.showAsDropDown(view);}

menu_pop_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="120dp">
    <LinearLayout
        android:layout_width="80dp"
        android:layout_height="120dp"
        android:orientation="vertical"
        android:background="@drawable/round_corner_image_by_atool"
        >
        <Button
            android:id="@+id/action_setup"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="设置"
            android:textColor="#ffffff"
            android:background="@null"
           />
    </LinearLayout>
</RelativeLayout>

展开阅读全文

没有更多推荐了,返回首页