popupwindow弹出窗关闭_PopupWindow弹出框

使用PopupWindow实现一个悬浮框,悬浮在Activity之上,显示位置可以指定

首先创建pop\_window.xml:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

android:background="#bbbbbb"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_marginLeft="8dp"

android:layout_marginTop="8dp"

android:layout_marginEnd="8dp"

android:layout_marginRight="8dp"

android:layout_marginBottom="8dp"

app:layout_constraintBottom_toTopOf="@+id/tv_msg"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/tv_msg"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_marginLeft="8dp"

android:layout_marginTop="8dp"

android:layout_marginEnd="8dp"

android:layout_marginRight="8dp"

android:layout_marginBottom="8dp"

app:layout_constraintBottom_toTopOf="@+id/linearLayout"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/linearLayout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_marginLeft="8dp"

android:layout_marginTop="8dp"

android:layout_marginEnd="8dp"

android:layout_marginRight="8dp"

android:layout_marginBottom="8dp"

android:orientation="horizontal"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="1.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.625">

android:id="@+id/btn_cancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="取消" />

android:id="@+id/btn_ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="确定" />

使用ConstraintLayout看起来比较麻烦,用拖拽还是很方便的。这里设置四个组件,两个TextView,两个Button

activity\_main.xml:

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/btn_pop_window"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_marginLeft="8dp"

android:layout_marginTop="8dp"

android:layout_marginEnd="8dp"

android:layout_marginRight="8dp"

android:layout_marginBottom="8dp"

android:text="pop_window"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

主界面放置一个按钮。

MainActivity:

package com.fitsoft;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.PopupWindow;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button button;

PopupWindow popupWindow;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = findViewById(R.id.btn_pop_window);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

popupWindow.showAsDropDown(v);

}

});

//载入界面

View view = LayoutInflater.from(this).inflate(R.layout.pop_window, null);

{

TextView textView = view.findViewById(R.id.tv_title);

textView.setText("标题");

}

{

TextView textView = view.findViewById(R.id.tv_msg);

textView.setText("这里是popwindow的消息内容");

}

{

Button button = view.findViewById(R.id.btn_ok);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(v.getContext(), "您点击了确定按钮", Toast.LENGTH_SHORT).show();

popupWindow.dismiss();

}

});

}

{

Button button = view.findViewById(R.id.btn_cancel);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(v.getContext(), "您点击了取消按钮", Toast.LENGTH_SHORT).show();

popupWindow.dismiss();

}

});

}

//绑定

popupWindow = new PopupWindow(view, WindowManager.LayoutParams.WRAP_CONTENT,

WindowManager.LayoutParams.WRAP_CONTENT);

}

}

这里调用了PopupWindow的构造方法:

/**

*

Create a new non focusable popup window which can display the

* contentView. The dimension of the window must be passed to

* this constructor.

*

*

The popup does not provide any background. This should be handled

* by the content view.

*

* @param contentView the popup's content

* @param width the popup's width

* @param height the popup's height

*/

public PopupWindow(View contentView, int width, int height) {

this(contentView, width, height, false);

}

绑定视图,设置宽高...很好用...

最后用按钮的点击事件弹出窗口:

popupWindow.showAsDropDown(v);

效果图:

![zxWK1kjy4YarLhb.png][]

左对齐,显示在下方....调整一下弹出代码,将刚刚的弹出代码注释掉,用下面的代码替换:

popupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(),

Gravity.CENTER, 0, 0);

获取顶级布局,设置居中显示,x、y不偏移。

效果图:

![ZzuheCjTl9sRqft.gif][]

[zxWK1kjy4YarLhb.png]: /images/1609994206717.png

[ZzuheCjTl9sRqft.gif]: /images/1609994196511.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值