MainActivity.java
package com.example.mybottompopupwindowdemo; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.view.ViewPager.LayoutParams; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.ScaleAnimation; import android.view.animation.Animation.AnimationListener; import android.widget.Button; import android.widget.PopupWindow; import android.widget.RelativeLayout; public class MainActivity extends Activity implements OnClickListener { private Button button; private PopupWindow mcontactsBottompopup; private View contactBottomPopulayout; private RelativeLayout ll_contact_root; private RelativeLayout rl_contact_copy; private RelativeLayout rl_contact_send; private RelativeLayout rl_contact_cancle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); ll_contact_root = (RelativeLayout) findViewById(R.id.ll_contact_root); initContactsBottmoPopu(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mcontactsBottompopup.isShowing()) { mcontactsBottompopup.dismiss();// 关闭 } else { mcontactsBottompopup.showAtLocation(MainActivity.this .findViewById(R.id.ll_contact_root),Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0); // 动画效果 WindowManager.LayoutParams params = getWindow() .getAttributes(); params.alpha = 0.7f; getWindow().setAttributes(params); ScaleAnimation animation = new ScaleAnimation(1f,1f,0f,Animation.RELATIVE_TO_SELF,0.5f,1.0f); animation.setDuration(150); animation.setFillAfter(true); contactBottomPopulayout.startAnimation(animation); } } }); mcontactsBottompopup .setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams params = getWindow() .getAttributes(); params.alpha = 1.0f; getWindow().setAttributes(params); ScaleAnimation animation = new ScaleAnimation(1f,1.0f); animation.setDuration(150); animation.setFillAfter(true); contactBottomPopulayout.startAnimation(animation); } }); } private void initContactsBottmoPopu() { contactBottomPopulayout = View.inflate(this,R.layout.contacts_bottom_popup,null); mcontactsBottompopup = new PopupWindow(contactBottomPopulayout); mcontactsBottompopup.setContentView(contactBottomPopulayout); // 加上这个popupwindow中的ListView才可以接收点击事件 mcontactsBottompopup.setWidth(LayoutParams.FILL_PARENT); mcontactsBottompopup.setHeight(LayoutParams.WRAP_CONTENT); mcontactsBottompopup.setFocusable(true); // ColorDrawable dw = new ColorDrawable(0xb0000000); mcontactsBottompopup.setBackgroundDrawable(new BitmapDrawable()); mcontactsBottompopup .setAnimationStyle(R.style.contactPopuAnimationExit); mcontactsBottompopup.setOutsideTouchable(true); rl_contact_copy = (RelativeLayout) contactBottomPopulayout .findViewById(R.id.rl_contact_copy); rl_contact_send = (RelativeLayout) contactBottomPopulayout .findViewById(R.id.rl_contact_send); rl_contact_cancle = (RelativeLayout) contactBottomPopulayout .findViewById(R.id.rl_contact_cancle); rl_contact_copy.setOnClickListener(this); rl_contact_send.setOnClickListener(this); rl_contact_cancle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.rl_contact_copy: contactBottomPopuExit(); break; case R.id.rl_contact_send: contactBottomPopuExit(); break; case R.id.rl_contact_cancle: contactBottomPopuExit(); break; default: break; } } private void contactBottomPopuExit() { ScaleAnimation animation = new ScaleAnimation(1f,1.0f); animation.setDuration(150); animation.setFillAfter(true); contactBottomPopulayout.startAnimation(animation); WindowManager.LayoutParams params = getWindow().getAttributes(); params.alpha = 1.0f; getWindow().setAttributes(params); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mcontactsBottompopup.dismiss();// 关闭 } }); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ll_contact_root" tools:context="com.example.mybottompopupwindowdemo.MainActivity" > <Button android:id="@+id/button" android:text="点击我,从底部滑出popupwindow" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>contacts_bottom_popup.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#ebebe9" android:orientation="vertical" > <RelativeLayout android:background="@drawable/bottom_popu_bg_selector" android:id="@+id/rl_contact_copy" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="8dp" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/iv_contact_copy" android:text="复制号码" android:textColor="#111111" android:textSize="16dp" /> <ImageView android:id="@+id/iv_contact_copy" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="18dp" android:background="@drawable/iv_contact_copy" /> </RelativeLayout> <RelativeLayout android:background="@drawable/bottom_popu_bg_selector" android:id="@+id/rl_contact_send" android:layout_width="match_parent" android:layout_height="48dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/iv_contact_send" android:text="发送号码" android:textColor="#111111" android:textSize="16dp" /> <ImageView android:id="@+id/iv_contact_send" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="17dp" android:background="@drawable/iv_contact_send" /> </RelativeLayout> <RelativeLayout android:background="@drawable/bottom_popu_bg_selector" android:id="@+id/rl_contact_cancle" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginBottom="8dp" > <ImageView android:id="@+id/iv_contact_cancle" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="17dp" android:background="@drawable/iv_contact_cancle" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/iv_contact_cancle" android:text="取消" android:textColor="#111111" android:textSize="16dp" /> </RelativeLayout> </LinearLayout>
bottom_popu_bg_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/more_popu_pressed"/> <item android:drawable="@drawable/contact_bottom_popu_normal" /> </selector>
contactpopu.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 位移效果 fromXDelta="0%p" 指的x轴是相对于父控件从父控件的x=0出开始位移 fromYDelta="0%p" 指的y轴是相对于父控件从父控件的y=0出开始位移 toXDelta="100%p" 指的x轴是相对于父控件到达父控件的x=100的位置即x轴在屏幕的终点 toYDelta="100%p" 指的y轴是相对于父控件到达父控件的y=100的位置即x轴在屏幕的终点 --> <scale android:duration="250" android:pivotX="50%" android:pivotY="100%" android:fromXScale="1.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="0.0" /> </set>
color.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="more_popu_pressed">#d9d9d9</drawable> <drawable name="contact_bottom_popu_normal">#ebebe9</drawable> </resources>
styles.xml
<style name="contactPopuAnimationExit"> <item name="android:windowExitAnimation">@anim/contactpopu</item> </style>
--------------------------------------------------或者---------------------------------------------------
activity_main.xml
@H_502_30@<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:padding="@dimen/padding_medium" android:text="点击我弹出选择框" tools:context=".MainActivity" /> </RelativeLayout>alert_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <LinearLayout android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/btn_style_alert_dialog_background" android:gravity="center_horizontal" android:orientation="vertical" > <Button android:id="@+id/btn_take_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:background="@drawable/btn_style_alert_dialog_button" android:text="拍照" android:textStyle="bold" /> <Button android:id="@+id/btn_pick_photo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="5dip" android:background="@drawable/btn_style_alert_dialog_button" android:text="从相册选择" android:textStyle="bold" /> <Button android:id="@+id/btn_cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dip" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="15dip" android:background="@drawable/btn_style_alert_dialog_cancel" android:text="取消" android:textColor="#ffffff" android:textStyle="bold" /> </LinearLayout> </RelativeLayout>
MainActivity
package com.example.picpopupwindow; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity { SelectPicPopupWindow menuWindow; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) this.findViewById(R.id.text); tv.setOnClickListener(new OnClickListener() { public void onClick(View v) { menuWindow = new SelectPicPopupWindow(MainActivity.this,itemsOnClick); menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main),Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0); } }); } private OnClickListener itemsOnClick = new OnClickListener(){ public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()) { case R.id.btn_take_photo: break; case R.id.btn_pick_photo: break; default: break; } } }; }
SelectPicPopupWindow
package com.example.picpopupwindow; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; public class SelectPicPopupWindow extends PopupWindow { private Button btn_take_photo,btn_pick_photo,btn_cancel; private View mMenuView; public SelectPicPopupWindow(Activity context,OnClickListener itemsOnClick) { super(context); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.alert_dialog,null); btn_take_photo = (Button) mMenuView.findViewById(R.id.btn_take_photo); btn_pick_photo = (Button) mMenuView.findViewById(R.id.btn_pick_photo); btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel); btn_cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { dismiss(); } }); btn_pick_photo.setOnClickListener(itemsOnClick); btn_take_photo.setOnClickListener(itemsOnClick); this.setContentView(mMenuView); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setFocusable(true); this.setAnimationStyle(R.style.AnimBottom); ColorDrawable dw = new ColorDrawable(0xb0000000); this.setBackgroundDrawable(dw); mMenuView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v,MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop(); int y=(int) event.getY(); if(event.getAction()==MotionEvent.ACTION_UP){ if(y<height){ dismiss(); } } return true; } }); } }
styles.xml
<resources> <style name="AppTheme" parent="android:Theme.Light" /> <style name="AnimBottom" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/push_bottom_in</item> <item name="android:windowExitAnimation">@anim/push_bottom_out</item> </style> <style name="PopupAnimation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/push_bottom_in</item> <item name="android:windowExitAnimation">@anim/push_bottom_out</item> </style> </resources>
push_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑入式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="200" /> </set>
push_bottom_out.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 上下滑入式 --> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="200" /> </set>原文链接:https://www.f2er.com/xml/296713.html