PopupWindow是Android上自定义弹出窗口,使用起来很方便。
1.PopupWindow是一个可以显示在当前Activity之上的浮动容器
2.可以使用任意布局的View作为其内容
3.PopupWindow弹出的位置是能够改变的
PopupWindow使用
1.创建PopupWindow
PopupWindow pop = new PopupWindow(view contentView,int width,int height,boolean focuse);
2.显示PopupWindow
PopupWindow.showAsDropDown(View anchor)
PopupWindow.showAsDropDown(View anchor,int xoff,int yoff)
PopupWindow.showAtLocation(View parent,int gravity,int x,int y)
3.关闭PopupWindow
PopupWindow.dismiss()
显示PopupWindow方式
1.显示在一个参照物View的周围,有三个方法重载
showAsDropDown(View anchor)
showAsDropDown(View anchor,int xoff,int yoff)
showAsDropDown(View anchor, int xoff, int yoff, int gravity)(api 19)
2.显示在指定位置,有两个方法重载
showAtLocation(View parent,int gravity,int x,int y)
showAtLocation(IBinder token, int gravity, int x, int y)
现在开始代码!!
activity_main<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.com.popuw.MainActivity"> <TextView android:id="@+id/tv" android:text="+" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>popupwindow布局<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#ff0" android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv1" android:text="加好友" android:textSize="25sp" android:gravity="center" android:layout_width="86dp" android:layout_height="match_parent" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#ccc"/> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv2" android:text="扫一扫" android:textSize="25sp" android:gravity="center" android:layout_width="86dp" android:layout_height="match_parent" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#ccc"/> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:src="@mipmap/ic_launcher" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv3" android:text="搜索" android:textSize="25sp" android:gravity="center" android:layout_width="86dp" android:layout_height="match_parent" /> </LinearLayout> </LinearLayout>
main方法public class MainActivity extends AppCompatActivity { TextView tv; PopupWindow popu; RelativeLayout relativelyout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 tv = (TextView) findViewById(R.id.tv); relativelyout = (RelativeLayout) findViewById(R.id.activity_main); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击后弹出popu showpopu(relativelyout); } }); } private void showpopu(View v) { View view=View.inflate(this,R.layout.popu,null); // 创建popu对象 //参数一:popu需要显示的布局视图 参数二:视图的宽 参数三:视图的高 popu = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); //首先让pop页面可以获取焦点,只有在获取焦点后点击返回按钮 按钮才能取消pop页面 popu.setFocusable(true); //pop界面以外的位置可以点击 点击后pop页面关闭 popu.setOutsideTouchable(true); //关闭pop页面 popu.setBackgroundDrawable(new BitmapDrawable()); //指定pop在指定的控件下显示 // popu.showAsDropDown(v); //pop借助父控件显示 y 下 x左 popu.showAtLocation(v, Gravity.CENTER,0,0); //给pop页面中的控件添加监听 TextView tv1 = view.findViewById(R.id.tv1); TextView tv2 = view.findViewById(R.id.tv2); TextView tv3 = view.findViewById(R.id.tv3); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"加好友",Toast.LENGTH_SHORT).show(); //关闭pop页面,调用dismiss方法 popu.dismiss(); } }); tv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"加好友",Toast.LENGTH_SHORT).show(); //关闭pop页面,调用dismiss方法 popu.dismiss(); } }); tv3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this,"加好友",Toast.LENGTH_SHORT).show(); //关闭pop页面,调用dismiss方法 popu.dismiss(); } }); } }