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

来源:互联网 发布:vlad算法 编辑:程序博客网 时间:2024/05/16 15:11

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 =getLayoutInflater().inflate( 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好用多了,这个还可以随意设置大小。有什么不懂的可以留言给我,我会在第一时间回复。。。。。

阅读全文
0 0