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



原创粉丝点击