PopupWindow底部弹出

时间:2022-01-30 02:36:50

说明:从屏幕底部弹出PopupWindow,有弹出隐藏动画效果.背景设置透明度.

效果图例如以下:

PopupWindow底部弹出



1.MainActivity.java   显示popwindow,宽高跟屏幕大小一样,设置一个透明度背景

public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new OnClickListener() { public void onClick(View v) { showPopwindow(); } }); } private void showPopwindow() { View parent = ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0); View popView = View.inflate(this, R.layout.camera_pop_menu, null); Button btnCamera = (Button) popView.findViewById(R.id.btn_camera_pop_camera); Button btnAlbum = (Button) popView.findViewById(R.id.btn_camera_pop_album); Button btnCancel = (Button) popView.findViewById(R.id.btn_camera_pop_cancel); int width = getResources().getDisplayMetrics().widthPixels; int height = getResources().getDisplayMetrics().heightPixels; final PopupWindow popWindow = new PopupWindow(popView,width,height); popWindow.setAnimationStyle(R.style.AnimBottom); popWindow.setFocusable(true); popWindow.setOutsideTouchable(false);// 设置同意在外点击消失 OnClickListener listener = new OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.btn_camera_pop_camera: break; case R.id.btn_camera_pop_album: break; case R.id.btn_camera_pop_cancel: break; } popWindow.dismiss(); } }; btnCamera.setOnClickListener(listener); btnAlbum.setOnClickListener(listener); btnCancel.setOnClickListener(listener); ColorDrawable dw = new ColorDrawable(0x30000000); popWindow.setBackgroundDrawable(dw); popWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); } }

2.camera_pop_menu.xml   Popupwindow载入的布局文件

<?

xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" android:layout_width="wrap_content" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="10dp" android:orientation="vertical" > <Button android:id="@+id/btn_camera_pop_camera" style="@style/txt_camera_pop_menu" android:layout_width="match_parent" android:layout_height="45dp" android:background="@drawable/pop_first_selector" android:text="@string/camera_pop_camera" android:textSize="18sp" /> <Button android:id="@+id/btn_camera_pop_album" style="@style/txt_camera_pop_menu" android:layout_width="match_parent" android:layout_height="45dp" android:background="@drawable/pop_last_selector" android:text="@string/camera_pop_album" android:textSize="18sp" /> <Button android:id="@+id/btn_camera_pop_cancel" style="@style/txt_camera_pop_menu" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginTop="10dp" android:background="@drawable/pop_single_selector" android:text="@string/camera_pop_cancel" android:textSize="18sp" /> </LinearLayout> </RelativeLayout>


点击下载源代码

各位看官假设认为文章不错,帮忙点个赞吧。对于你来说是举手之劳,但对于我来说这就是坚持下去的动力。

假设你想第一时间看我们的后期文章。扫码关注公众号。每周不定期推送Android开发实战教程文章,你还等什么,赶快关注吧,学好技术,,出任ceo。赢取白富美。

。。。

Android开发666 - 安卓开发技术分享 扫描二维码加关注

PopupWindow底部弹出