PopupWindow菜单实现父窗口遮罩

来源:互联网 发布:淘宝连衣裙店铺推荐 编辑:程序博客网 时间:2024/05/01 16:40

重点内容在注释中体现:全屏显示一个页面,将这个页面背景设置成半透明,菜单控件显示在屏幕下方,即可完成popupwindow菜单,并实现了父窗口遮罩

popupwindow xml文件 login_type_popupwindow.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#b0000000" ><!-全屏灰色半透明->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:layout_alignParentBottom="true"        android:layout_marginBottom="10dp" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_marginRight="10dp"            android:background="@drawable/common_white_bg"            android:orientation="vertical" >            <TextView                android:id="@+id/mServerLogin"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center_horizontal"                android:paddingBottom="8dp"                android:paddingTop="8dp"                android:text="@string/serverLogin"                android:textColor="#3995e1"                android:textSize="@dimen/title_text_size" />            <LinearLayout                android:layout_width="match_parent"                android:layout_height="0.5dp"                android:background="#999999" />            <TextView                android:id="@+id/mSmsLogin"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center_horizontal"                android:paddingBottom="8dp"                android:paddingTop="8dp"                android:text="@string/messageLogin"                android:textColor="#3995e1"                android:textSize="@dimen/title_text_size" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:layout_marginRight="10dp"            android:layout_marginTop="15dp"            android:background="@drawable/common_white_bg_select"            android:orientation="vertical" >            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:gravity="center_horizontal"                android:paddingBottom="8dp"                android:paddingTop="8dp"                android:text="@string/cancel"                android:textColor="#3995e1"                android:textSize="@dimen/title_text_size" />        </LinearLayout>    </LinearLayout></RelativeLayout>

style.xml

 <!-- popupwindow 动画 -->    <style name="animation">        <item name="android:windowEnterAnimation">@anim/enter</item>        <item name="android:windowExitAnimation">@anim/out</item>    </style>

enter_anim.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false"> <translate        android:fromYDelta="100%p"        android:toYDelta="0"         android:duration="500"        /> <alpha        android:fromAlpha="0.7"        android:toAlpha="1.0"         android:duration="300"        /> </set>

out_anim.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="false"> <translate        android:fromYDelta="0"        android:toYDelta="100%p"         android:duration="3000"        /> <alpha        android:fromAlpha="1.0"        android:toAlpha="0.5"         android:duration="2000"        /> </set>

下面是java代码

//初始化方法private void InitPopupWindow() {View contentView = getLayoutInflater().inflate(R.layout.login_type_popwindow, null);//动态加载TextView mServerLogin = (TextView) contentView.findViewById(R.id.mServerLogin);TextView mSmsLogin = (TextView) contentView.findViewById(R.id.mSmsLogin);mServerLogin.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (popupWindow.isShowing())popupWindow.dismiss();}});mSmsLogin.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (popupWindow.isShowing())popupWindow.dismiss();}});popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏显示,将内容设置在底部popupWindow.setFocusable(true);popupWindow.setBackgroundDrawable(new BitmapDrawable());popupWindow.setAnimationStyle(R.style.animation);parent = this.findViewById(R.id.activity_login);//父窗口view}

使用方法在oncreate()方法中调用InitPopupWindow();然后再需要弹出popupwindow的空间点击时间中调用popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);即可!





1 0
原创粉丝点击