PopupWindow的基本使用
来源:互联网 发布:紫薇圣人 知乎 编辑:程序博客网 时间:2024/06/02 10:28
今天用popupwindow实现这样一个小效果
先了解下基本用法:
设置个Button,点击时,显示popupwindow,布局:
<Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="popup" android:layout_centerInParent="true" android:text="PopupWindow"/>设置一个textview,展示文字
TextView tv = new TextView(getApplicationContext()); tv.setText("我是弹出窗口"); tv.setBackgroundColor(Color.RED); //参数1,要显示的view;参数2,3,宽和高 PopupWindow popupWindow = new PopupWindow(tv, 100, 100); //显示在某个View的左下角 popupWindow.showAsDropDown(mBtn);
//显示在某个View的左下角,并设置偏移量 popupWindow.showAsDropDown(mBtn,50,50);
//参数1,要显示的view;参数2,3,宽和高 PopupWindow popupWindow = new PopupWindow(tv, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);
MainActivity has leaked window android.widget.TextView设置焦点和背景,实现点击其他地方和返回键,popupwindow消失,并且不会有内存泄漏问题
//参数1,要显示的view;参数2,3,宽和高 PopupWindow popupWindow = new PopupWindow(tv, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT); //设置popupwindow获得焦点 popupWindow.setFocusable(true); //设置背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //显示在某个View的左下角,并设置偏移量 popupWindow.showAsDropDown(mBtn,50,50);
然后我们就可以实现开始的的那种效果了,代码如下
// 一个自定义的布局,作为显示的内容 View contentView = LayoutInflater.from(this).inflate( R.layout.tips_popwindow, null); //参数1,要显示的view;参数2,3,宽和高 PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); //设置popupwindow获得焦点 popupWindow.setTouchable(true); //设置背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //设置动画 popupWindow.setAnimationStyle(R.style.popwin_anim_style); //设置居中位置和偏移量 popupWindow.showAtLocation(mBtn, Gravity.CENTER, 0, 0);tips_popwindow的布局:
<LinearLayout android:id="@+id/layout_show_tips" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="visible" android:gravity="center_vertical|right" android:orientation="horizontal"> <ImageButton android:id="@+id/btn_show_tips" android:layout_width="22.5dp" android:layout_height="25dp" android:background="@mipmap/tips_show" android:scaleType="fitXY"/> <TextView android:id="@+id/tv_show_tips" android:layout_width="wrap_content" android:layout_height="match_parent" android:textSize="14sp" android:gravity="center_vertical" android:paddingRight="3dp" android:paddingLeft="3dp" android:background="#ff7e2a" android:text="我是温馨小提示"/> </LinearLayout>style中设置动画:
<style name="popwin_anim_style"> <item name="android:windowEnterAnimation">@anim/tips_show</item> <item name="android:windowExitAnimation">@anim/tips_hide</item> </style>anim下的显示和隐藏动画:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="0" android:duration="200" /></set>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0" android:toYDelta="0" android:duration="200" /></set>实现了开始的效果
1 0
- PopupWindow的基本使用
- PopupWindow 的基本使用
- PopupWindow 的基本使用
- PopupWindow的最基本使用
- Android中PopupWindow的基本使用
- PopupWindow(悬浮框)的基本使用
- Android中PopupWindow的基本使用
- 关于android中PopupWindow的基本使用
- popupWindow基本使用
- popupwindow的基本使用以及基本动画效果
- PopupWindow的基本使用方法
- PopupWindow的基本操作
- PopupWindow的基本用法
- Android中的PopupWindow基本使用
- Android PopupWindow的使用
- Android PopupWindow的使用
- Android PopupWindow的使用!!!
- 关于popupWindow的使用
- codevs1034(网络流)
- String作为参数传递 * StringBuffer作为参数传递
- boost库在工作(1) 使用数组智能指针scoped_array
- HDU 1428 漫步校园(BFS+DFS)
- Binary Tree Level Order Traversal II
- PopupWindow的基本使用
- 相关分析
- orbslam2-基础理论(五)ransac
- Java设计模式--装饰模式
- java中通过JDK工具手动压缩JAR包时package路径必须严格对应,否则可能压缩JAR包成功,但执行不了
- 虚函数表解析
- Java猜拳小游戏源代码
- 【English】回归
- WEB开发的会话跟踪方式