PopupWindows使用起来特别简单,就不再给大家分析了,直接上代码了
功能代码
public class PopupWindows extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_windows); } public void popupwindows(View v){ //实例化popupwindos中的子布局文件 View view = getLayoutInflater().inflate(R.layout.popupwindows_layout,null); //给弹窗中的按钮设置点击事件 Button popupButton = (Button) view.findViewById(R.id.popup_btn); popupButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(PopupWindows.this,"哦,吼吼,它被点击啦...",Toast.LENGTH_SHORT).show(); } }); //popupwindows中的view对象需要自定义xml文件 PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); //设置弹窗的宽高度 // PopupWindow popupWindow = new PopupWindow(view, 200,200); //设置弹窗的自定义宽高度 //为方便测试,popupwindows中使用的资源引用安卓系统自带的资源 popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_star_big_on)); //设置背景图片 popupWindow.setAnimationStyle(android.R.style.Animation_Translucent); //设置弹出的动画效果(可以自定义效果) popupWindow.getBackground().setAlpha(100); //设置透明度(0-255之间) popupWindow.setOutsideTouchable(true); //设置点击边缘区域windows消失 popupWindow.setFocusable(true); popupWindow.setTouchable(true); popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); //设置软键盘的弹出自适应 popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0); //设置popupWindows的显示位置,0,0代表偏移量 //获取屏幕尺寸 DisplayMetrics dm =new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); //获取宽高 int width = dm.widthPixels; int height = dm.heightPixels; // // //第二种方式,获取当前activity的宽高度,如果当前的activity不是全屏显示,获取的数据可能会有误差 // int width = getWindowManager().getDefaultDisplay().getWidth(); // int height = getWindowManager().getDefaultDisplay().getHeight(); } }
布局文件需要单独拿出来说一下,因为popupWindows弹出的窗口是一个独立的layout布局文件,所以需要使用
View view = getLayoutInflater().inflate(R.layout.popupwindows_layout,null);
实例化一个View对象填充popupWindows
布局分两部分,一个主activity的布局简单的一个按钮
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.zhuandian.msuic.PopupWindows"> <Button android:onClick="popupwindows" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="测试弹窗" /> </RelativeLayout>
popupWIndows的布局文件,里面的内容可根据需要自己定义,当然对里面控件的操作也可以根据需求具体自己实现
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/a" /> <Button android:id="@+id/popup_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:text="点击"/> </FrameLayout>