PopupWindow弹窗详解以及相关方法说明

最近做到在项目中用到,在这个分享给大家

layout_detail_traffic_item.xml文件中:其中text随便填就好,我这里是引用string.xml文件中的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="500dp"
    android:layout_height="200dp"
    android:background="#EDEDED"
    android:orientation="vertical" >
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    </TextView>

    <TextView
        android:id="@+id/detail_traffic_day"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:text="@string/detail_traffic_text_day"
        android:textColor="#404040"
        android:textSize="14dp" />
    
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    </TextView>

    <TextView
        android:id="@+id/detail_traffic_week"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:text="@string/detail_traffic_text_week"
        android:textColor="#404040"
        android:textSize="14dp" />
    
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    </TextView>

    <TextView
        android:id="@+id/detail_traffic_month"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:text="@string/detail_traffic_text_month"
        android:textColor="#404040"
        android:textSize="14dp" />
    
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    </TextView>

</LinearLayout>

在你想要点击的某个控件后弹出这个窗口,则在这个控件的onclick方法中的相应位置添加以下代码:

JAVA代码:

View contentView = PluginResUtil.getInstance().inflate(mContext, R.layout.layout_detail_traffic_item, null); 
            PopupWindow popWnd = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);  
            // 需要设置一下此参数,点击外边可消失  
            popWnd.setBackgroundDrawable(new BitmapDrawable());  
            // 需要设置一下此参数,点击外边可消失  
            popWnd.setOutsideTouchable(true);      

            TextView textDay = (TextView)contentView.findViewById(R.id.detail_traffic_day);
            TextView textWeek = (TextView)contentView.findViewById(R.id.detail_traffic_week);
            TextView textMonth = (TextView)contentView.findViewById(R.id.detail_traffic_month);

            textDay.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                   //做你想做的事
                }
            });

            textWeek.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    //做你想做的事
                }
            });

            textMonth.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                      //做你想做的事
                }
            });
            //显示在某控件的下方
            popWnd.showAsDropDown(v);

 

亲测很好用,比popupmenu好用多了,这个还可以随意设置大小。有什么不懂的可以留言给我,我会在第一时间回复。。。。。

 

 

转载于:https://my.oschina.net/u/2490453/blog/791558

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值