PopupWindow的使用
来源:互联网 发布:乐陵淘宝客服打包 编辑:程序博客网 时间:2024/05/18 03:52
PopupWindow
一、什么是PopupWindow
PopupWindow在app中经常能看到,官网上面的定义是这样:
PopupWindow是一个能用来显示任意视图且浮动在当前Activity之上的容器。
功能和Dialog是不是很类似?
二、PopupWindow的基本使用
先上效果图:
代码里面注释很详细,这里就不说明了
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener { private Button mButton01; private Button mButton02; private Button mButton03; private Button mButton04; private PopupWindow mPopupWindow; private int mScreenWidth; private int mPopupWindowWidth; private int mPopupWindowHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButton01 = (Button) findViewById(R.id.button01); mButton02 = (Button) findViewById(R.id.button02); mButton03 = (Button) findViewById(R.id.button03); mButton04 = (Button) findViewById(R.id.button04); mButton01.setOnClickListener(this); mButton02.setOnClickListener(this); mButton03.setOnClickListener(this); mButton04.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ // 相对某个控件的位置(正左下方),无偏移 case R.id.button01: getPopupWindowInstance(); mPopupWindow.showAsDropDown(view); break; // 相对某个控件的位置(正左下方),有偏移 case R.id.button02: getPopupWindowInstance(); mPopupWindow.showAsDropDown(view, 50, 50);// X、Y方向各偏移50 break; // 相对于父控件的位置,无偏移 case R.id.button03: getPopupWindowInstance(); mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); break; // 相对于父控件的位置,有偏移 case R.id.button04: getPopupWindowInstance(); mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 50); break; } } /** * 获取popupWindow实例 */ private void getPopupWindowInstance() { if (null != mPopupWindow) { mPopupWindow.dismiss(); return; } else { initPopuptWindow(); } } /** * 创建popupwindow */ private void initPopuptWindow() { View popupWindow = LayoutInflater.from(this).inflate(R.layout.popup_window, null); RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(this); // 创建一个PopupWindow // 参数1:contentView 指定PopupWindow的内容 // 参数2:width 指定PopupWindow的width // 参数3:height 指定PopupWindow的height mPopupWindow = new PopupWindow(popupWindow, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); //popupwindow之外点击消失 mPopupWindow.setOutsideTouchable(true); //设置背景才有效果 mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); // 获取屏幕和PopupWindow的width和height mScreenWidth = getWindowManager().getDefaultDisplay().getWidth(); mScreenWidth = getWindowManager().getDefaultDisplay().getHeight(); mPopupWindowWidth = mPopupWindow.getWidth(); mPopupWindowHeight = mPopupWindow.getHeight(); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { mPopupWindow.dismiss(); }}
activity_mian.xml文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/activity_main" 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:orientation="vertical" tools:context="com.ymq.popupwindowdemo.MainActivity"> <Button android:id="@+id/button01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="以自己为Anchor,不偏移" /> <Button android:id="@+id/button02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="以自己为Anchor,有偏移" /> <Button android:id="@+id/button03" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="以屏幕中心为参照,不偏移(正中间)" /> <Button android:id="@+id/button04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="以屏幕下方为参照,下方中间" /></LinearLayout>
popup_window.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/colorPrimaryDark" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择状态:" android:textColor="@android:color/white" android:textSize="20px" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="在线" /> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="离线" /> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="隐身" /> </RadioGroup></LinearLayout>
方法showAsDropDown()
方法的作用就是用来显示PopupWindow,在文档里面有详细的说明。
//在锚点的左下角显示PopupWindow,无偏移showAsDropDown(View anchor) //以锚点左下角为原点指定偏移量的位置显示PopupWindowshowAsDropDown(View anchor, int xoff, int yoff)//已父容器为锚点显示showAsDropDown(View anchor, int gravity,int xoff, int yoff)
总结:
- 创建PopupWindow需要显示的视图,当然我们也可以在后面通过
setContentView(View contentView)
方法为popupwindow设置视图。 创建popupwindow对象,可以创建空内容的,后面自己设置视图,也可以将第一步创建好的视图传进去,还可以指定其宽高。对应的构造方法如下:
PopupWindow() //Create a new empty, non focusable popup window of dimension (0,0). PopupWindow(Context context) //Create a new empty, non focusable popup window of dimension (0,0). PopupWindow(View contentView) //Create a new non focusable popup window which can display the contentView. PopupWindow(int width, int height) Create a new empty, non focusable popup window. PopupWindow(View contentView, int width, int height) //Create a new non focusable popup window which can display the contentView.
为popupwindow设置一些属性,比如
setAnimationStyle(int animationStyle)setBackgroundDrawable(Drawable background)setElevation(float elevation)setOnDismissListener(PopupWindow.OnDismissListener onDismissListener)setWindowLayoutMode(int widthSpec, int heightSpec)setSoftInputMode(int mode)setOutsideTouchable(boolean touchable)
- 显示PopupWindow.
showAsDropDown(View anchor)
0 0
- Android PopupWindow的使用
- Android PopupWindow的使用
- Android PopupWindow的使用!!!
- 关于popupWindow的使用
- PopupWindow的使用
- PopupWindow的使用例子
- PopupWindow的使用
- popupWindow的使用心得
- PopupWindow的使用
- PopupWindow的使用
- Android PopupWindow的使用!!!
- PopupWindow的使用
- PopupWindow的使用
- popupWindow的使用
- PopupWindow的使用
- PopupWindow的使用
- Popupwindow的使用
- PopupWindow的使用
- Flink运行时之基于Netty的网络通信上
- 电路和电子学
- SKB 的分配细节.
- STM32外部中断设置
- CSS实现网页遮罩层功能
- PopupWindow的使用
- IOS 保存图片至相册
- MYeclipse使用theme
- Java内部类详解
- Eclipse的Java Working set使用方法
- Android VPN编程
- 关于中文命名静态资源导致乱码或者不读取的解决办法
- 网络爬虫基本原理(文章转自博客园:wawlian博客,文章很不错,转了一起学习)
- const的用法