有时候我们需要在界面上弹出一个窗口,而Android中弹出窗体有两种方式:一种是AlertDialog,另一种就是PopupWindow,AlertDialog的位置是固定的,而PopupWindow的位置可以任意指定。下面我们使用自定义的PopupWindow来完成以下的效果图:
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
/**
* 自定义悬浮窗PopupWindow
*/
public class CustomPopupWindow implements PopupWindow.OnDismissListener {
private PopupWindowListener mPopupWindowListener;
private PopupWindow mPopupWindow;
private Activity mActivity;
private View mParentView;
private int mWidth;
private int mHeight;
private View mPopupWindowView;
private boolean focusable;
public CustomPopupWindow(View parentView, Activity activity, View contentView, int width, int
height, boolean focusable) {
this.mActivity = activity;
this.mParentView = parentView;
this.mWidth = width;
this.mHeight = height;
this.focusable = focusable;
this.mPopupWindowView = contentView;
}
/**
* 显示PopupWindow
*/
public void showView() {
mPopupWindow = new PopupWindow(mPopupWindowView, mWidth, mHeight, focusable);
if (mPopupWindowListener != null) {
mPopupWindowListener.initView();
}
mPopupWindow.setBackgroundDrawable(new ColorDrawable(0xFFFFFF));
mPopupWindow.showAtLocation(mParentView, Gravity.CENTER | Gravity.CENTER, 0, 0);
mPopupWindow.update();
mPopupWindow.setOnDismissListener(this);
}
/**
* 点击悬浮窗外面时的操作
*/
@Override
public void onDismiss() {
setBackgroundAlpha(1f);
}
public interface PopupWindowListener {
// 初始化PopupWindow的控件
void initView();
}
public void setOnPopupWindowListener(PopupWindowListener listener) {
this.mPopupWindowListener = listener;
}
/**
* 隐藏PopupWindow
*/
public void dismiss() {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
mPopupWindow = null;
}
}
//设置屏幕背景透明效果
public void setBackgroundAlpha(float alpha) {
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
lp.alpha = alpha;
mActivity.getWindow().setAttributes(lp);
}
}
在MainActivity中:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CustomPopupWindow mCustomPopupWindow;
private ImageButton mImageButton;//悬浮窗的关闭按钮
private View mLayoutPopupWindowView;//悬浮窗的布局
private TextView mTvActivityRule;//悬浮窗的内容
private Button button;//点击悬浮窗的按钮
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn_activity_rule);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_activity_rule:
showPopupWindow();
break;
//关闭悬浮窗
case R.id.popupwindow_activity_rule_imgBtn:
mCustomPopupWindow.dismiss();
mCustomPopupWindow.setBackgroundAlpha(1);
break;
}
}
/**
* 显示悬浮窗,并设置背景透明度
*/
public void showPopupWindow() {
mLayoutPopupWindowView = LayoutInflater.from(this).inflate(R.layout
.popupwindow_activity_rule, null);
mCustomPopupWindow = new CustomPopupWindow(findViewById(R.id.linearlayout),
this, mLayoutPopupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout
.LayoutParams.WRAP_CONTENT, true);
mCustomPopupWindow.setOnPopupWindowListener(new CustomPopupWindow
.PopupWindowListener() {
// TODO 设置活动内容
@Override
public void initView() {
mImageButton = (ImageButton) mLayoutPopupWindowView.findViewById(R.id
.popupwindow_activity_rule_imgBtn);
mImageButton.setOnClickListener(MainActivity.this);
mTvActivityRule = (TextView) mLayoutPopupWindowView.findViewById(R.id
.popupwindow_activity_rule_text);
mTvActivityRule.setText("");
}
});
mCustomPopupWindow.showView();
mCustomPopupWindow.setBackgroundAlpha(0.3f);
}
}
其中popupwindow_activity_rule的布局为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="vertical">
<ScrollView
android:layout_width="300dp"
android:layout_height="400dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="5dp"
android:text="活动规则"
android:textColor="#000000"
android:textSize="18sp"/>
<ImageButton
android:id="@+id/popupwindow_activity_rule_imgBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:background="@null"/>
</RelativeLayout>
<TextView
android:id="@+id/popupwindow_activity_rule_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="14sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
其中ImageButton没有添加图片,可自行添加;
activity_main中添加一个按钮:R.id.btn_activity_rule,整个父窗体id为R.id.linearlayout;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:orientation="vertical">
<Button
android:id="@+id/btn_activity_rule"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:text="活动规则"
android:textSize="18sp"/>
</LinearLayout>