Android PopUpWindow简单使用

    xiaoxiao2021-12-14  19

    private void setPopupWindow(){ PopupWindow popupWindow; TextView cancel,sure; //获得自定义view View view = this.getLayoutInflater().inflate(R.layout.popup,null); sure = (TextView) view.findViewById(R.id.sure); cancel = (TextView) view.findViewById(R.id.cancel); popupWindow = new PopupWindow(); //必须设置自定义view popupWindow.setContentView(view); //必须设置高和宽 popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT); popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //设置动画所对应的style popupWindow.setAnimationStyle(R.style.Menu_popup); //相对某个控件的位置 与showAtLocation二选一 //popupWindow.showAsDropDown(show_btn); //相对于父控件的位置 显示本窗口的底部 View rootView = this.getLayoutInflater().inflate(R.layout.activity_main2,null); popupWindow.showAtLocation(rootView, Gravity.BOTTOM,0,0); cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); sure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); } 菜单:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#66000000"> <!--添加阴影,使PopupWindow的界面充满全屏,背景为#66000000, 列表菜单作为子菜单。--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/dialog"> <TextView android:layout_marginTop="24dp" android:id="@+id/sure" android:text="确认" android:gravity="center" android:layout_width="match_parent" android:layout_height="48dp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/cardview_dark_background"/> <TextView android:id="@+id/cancel" android:text="取消" android:gravity="center" android:layout_width="match_parent" android:layout_height="48dp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/cardview_dark_background"/> </LinearLayout> </LinearLayout> 动画style:

    <style name="Menu_popup" parent="@android:style/Animation.Activity"> <item name="android:windowEnterAnimation">@anim/menu_enter</item> <item name="android:windowExitAnimation">@anim/menu_exit</item> </style>进入动画: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"> <!-- 从底部进入--> <translate android:fromXDelta="0" android:fromYDelta="100%" android:toXDelta="0" android:toYDelta="0"/> </set>退出动画:

    <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000"> <!--从上向下移动--> <translate android:fromYDelta="0" android:fromXDelta="0" android:toYDelta="100%" android:toXDelta="0"/> </set>

    可参考详细内容:http://blog.csdn.net/harvic880925/article/details/49272285 

    转载请注明原文地址: https://ju.6miu.com/read-970771.html

    最新回复(0)