PopupWindow悬浮框

一般情况我们用的都是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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值