本帖最后由 liu 于 2019-1-3 10:55 编辑
PopupWindow的介绍和用法
![](http://www.sufeinet.com/static/image/hrline/1.gif)
一个弹出窗口控件,可以用来显示任意View,而且会浮动在当前activity的顶部
1、常用的构造方法
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)
2、常用方法
setContentView(View contentView):设置PopupWindow显示的View
getContentView():获得PopupWindow显示的View
showAsDropDown(View view):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View view, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y): 相对于父控件的位置
setWidth:设置宽度
setHeight:设置高度
setFocusable(true):设置焦点
setAnimationStyle(int drawable):设置动画效果
setOutsideTouchable(true):设置点击外部区域关闭窗口
setBackgroundDrawable(Drawable background):设置背景色
3、设置显示位置
showAsDropDown(View view):显示在某个指定控件的下方
showAsDropDown(View view, int xoff, int yoff):显示在某个指定控件的下方,可以设置偏移量
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置,可以设置偏移量
举例:
activity_main.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
tools:context="com.company.helloworld.test.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示弹窗" />
</LinearLayout>
layout_popwindow.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:text="提示"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_sign"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="是否退出?"
android:textColor="@android:color/black"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#F5F5F5" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_cancle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="取消"
android:textColor="@android:color/holo_blue_light"
android:textSize="16sp" />
<View
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:background="#E8E8E8" />
<TextView
android:id="@+id/tv_confirm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="确认"
android:textColor="@android:color/holo_blue_light"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity:
[Java] 纯文本查看 复制代码 import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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 {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show();
}
});
}
public void show() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View contentView = layoutInflater.inflate(R.layout.layout_popwindow, null);
final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
TextView tv_cancle = contentView.findViewById(R.id.tv_cancle);
tv_cancle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
TextView tv_confirm = contentView.findViewById(R.id.tv_confirm);
tv_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "你点击了确认", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable(
getResources(), (Bitmap) null));
popupWindow.setFocusable(true);
//在PopupWindow里面就加上下面代码,让键盘弹出时,不会挡住pop窗口。
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//添加pop窗口关闭事件
popupWindow.setOnDismissListener(new poponDismissListener());
backgroundAlpha(0.4f);
popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
}
/**
* 设置添加屏幕的背景透明度0.0-1.0 越小越不透明 越黑
*
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
class poponDismissListener implements PopupWindow.OnDismissListener {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
//Log.v("List_noteTypeActivity:", "我是关闭事件");
backgroundAlpha(1f);
}
}
}
效果如下图:
|