popuwindow 在Fragment的使用
popuwindow已经在Activity里面实现功能,但是移植到Fragment上各种Null异常、、、、新手求帮助这是已经在Activity实现的popuwindow 在Fragment上怎么菜能使用 要改哪些点
package com.example.ex01day01;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
public class PopuWindowActivity extends Activity {
private Button mButton;
private PopupWindow popupWindow;
private View contentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);
initpw();
}
private void initpw() {
contentView = getLayoutInflater().inflate(R.layout.layout_popu, null);
// popupWindow 显示的布局 大小 是否可点击
popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
}
public void btnPopu(View v) {
if (popupWindow.isShowing()) {
if (popupWindow!=null) {
popupWindow.dismiss();
}
}else {
initPoup();
}
}
private void initPoup() {
contentView.findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 关闭
popupWindow.dismiss();
android.view.WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1.0f;
getWindow().setAttributes(lp);
}
});
// 设置背景图片
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 设置进入退出动画
popupWindow.setAnimationStyle(R.style.PoPuWindonAnime);
// 设置显示在某个控件下
// popupWindow.showAsDropDown(mButton);
// 设置显示在布局中的位置
popupWindow.showAtLocation(mButton, Gravity.BOTTOM, 0, 0);
//设置屏幕透明度
android.view.WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.8f;
getWindow().setAttributes(lp);
//设置退出监听事件
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
android.view.WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;//设置屏幕不透明
getWindow().setAttributes(lp);
}
});
}
}