PopupWindow的基本使用

1>写好布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical" >
    
    <LinearLayout 
        android:id="@+id/edit_alarm"
        android:layout_width="match_parent"
        android:layout_height="60dp">
        
        <ImageView 
            android:layout_width="24dp"
            android:layout_marginLeft="16dp"
            android:layout_height="match_parent"
            android:src="@drawable/ic_alarm_add"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/edit_alarm"
            style="@style/popupwindow_text_style"/>
         
    </LinearLayout>
    
    <View  
        android:layout_width="220dp"  
        android:layout_height="1dp"
        android:background="?android:attr/listDivider" /> 
    <LinearLayout 
        android:id="@+id/clear_alarm"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        >
        <ImageView 
            android:layout_width="24dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="16dp"
            android:src="@drawable/ic_alarm_off"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/popupwindow_text_style"
            android:text="@string/clear_alarm" />
        
    </LinearLayout>
       

</LinearLayout>

 

2>代码:

public void showAlarmPopupWindow() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.alarm_popupwindow , null ,false);
        LinearLayout editLayout = (LinearLayout)view.findViewById(R.id.edit_alarm);
        LinearLayout clearLayout = (LinearLayout)view.findViewById(R.id.clear_alarm);

        final PopupWindow popupWindow = new PopupWindow(view , ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT , true);

        editLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                mPresenter.editAlarm();
            }
        });
        clearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                mPresenter.clearAlarm();
            }
        });
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        popupWindow.showAsDropDown(getActivity().findViewById(R.id.toolbar) , getActivity().getWindowManager().getDefaultDisplay().getWidth() - popupWindow.getWidth() , 1);
    }

 

posted @ 2016-06-12 11:08  Steve_Nash  阅读(229)  评论(0编辑  收藏  举报