PopupWindow悬浮框
来源:互联网 发布:金手指手机炒股软件 编辑:程序博客网 时间:2024/05/17 09:41
一般情况我们用的都是dialog对话框,悬浮框用的比较少,但是有些地方还必须要用悬浮框。像qq的长按弹出的置顶删除用的就是悬浮框。悬浮框与对话框唯一的区别在于它的位置是随意的。用起来也简单,加载一个view实例化后设置点东西就行了。
下面是代码和解析
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ public Button button; public PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button= (Button) findViewById(R.id.button); button.setOnClickListener(this); poppu(); } public void poppu(){ View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.item_pop,null,false); final EditText editText= (EditText) view.findViewById(R.id.edit); Button bu= (Button) view.findViewById(R.id.bu); //实例化一个popupwindow。参数是加载view已经宽和高 popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //设置为true则要它消失后才能响应其他事件 popupWindow.setFocusable(true); //为了点击非悬浮框处或者按返回键悬浮框消失,需要如下设置。 //而且必须设置一个背景才有效。 popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); bu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,editText.getText(),Toast.LENGTH_SHORT).show(); popupWindow.dismiss(); } }); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button: //让悬浮框在按钮下面x方向偏30位置显示(默认无偏是在左下方) popupWindow.showAsDropDown(v,30,0); break; } }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="@drawable/bg" android:layout_height="match_parent"> <EditText android:id="@+id/edit" android:layout_width="120dp" android:layout_height="60dp" /> <Button android:id="@+id/bu" android:layout_width="120dp" android:layout_height="wrap_content" android:text="按钮"/></LinearLayout>
除此之外再介绍一些方法
setAnimationStyle(int) 这个是设置动画效果的。里面的参数是一个int。很明显是传一个anim布局进去。
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAtLocation(View parent, int gravity, int x, int y): 相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移 PS:parent这个参数只要是activity中的view就可以了!
一些构造函数
public PopupWindow (Context context)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView)
public PopupWindow(View contentView, int width, int height, boolean focusable)
最后再提示一点。如果想要有上图那种气泡效果的话,把背景图做成.9图是个不错的办法。如何制作可以参考这篇文章
http://blog.csdn.net/lhp15575865420/article/details/75096560
阅读全文
0 0
- PopupWindow(悬浮框)示例
- PopupWindow悬浮框
- PopupWindow(悬浮框)的基本使用
- Android组件09—悬浮框PopupWindow
- Android组件09—悬浮框PopupWindow
- 2017最新详解Android中对话框之PopupWindow(悬浮框)
- popupWindow 实现悬浮半透明效果
- Android基础入门教程——2.5.5 PopupWindow(悬浮框)的基本使用
- android 在activity上的悬浮框、利用WindowManager和PopupWindow实现
- Android---悬浮框(PopupWindow)的弹出及屏幕变为半透明
- 消息提示类控件之PopupWindow(悬浮框)的基本使用
- Android中的悬浮效果PopupWindow详解
- Android使用Popupwindow实现悬浮菜单功能
- Android 自定义PopupWindow实现悬浮窗效果
- 悬浮框
- 悬浮框
- 悬浮框
- Android popupwindow以及windowManager总结——实现悬浮效果
- thinkphp mysql 读写分离
- SQL Server数据同步
- 搜索数据库存储过程或者函数中的内容
- 要将PDF文档进行文字排版需要使用什么PDF编辑软件
- p5.js入门教程(4) 鼠标交互
- PopupWindow悬浮框
- MCMC采样
- c# lambda表达式学习(1)
- Java对象池技术的原理及其实现
- Oracle merge into介绍
- ORB-SLAM2应用练习:三维重建系统搭建 (2)
- 某管理系统自定义审批流程实现
- 常用 Git 命令清单
- python面经总结之常见的问题与基本模块的常见使用方法