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

/**
 * 右上角菜单
 */
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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值