PoupWindow菜单
代码-控件下方:
PopupWindow popupWindow = new PopupWindow(Main2Activity.this);
View view= LayoutInflater.from(this).inflate(R.layout.layout_weixin_popupwindow,null);
popupWindow.setContentView(view);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(600);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(imageView,-100,-100);
1.实例化PopupWindow对象
2.setHeight----设置高度
setWidth----设置宽度
setOutsideTouchable----设置点击外部取消
showAsDropDown-----绑定控件,显示位置
代码-相对整个布局
View parent=LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main,null);
popupWindow.showAtLocation(parent, Gravity.BOTTOM,0,0);
对父布局进行实例化
showAtLocation----相对父布局
参数: 父布局,相对位置,x轴,y轴
代码-设置透明:
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha=0.5f;
getWindow().setAttributes(layoutParams);
对于弹出菜单的进行透明化设置
WindowManager.LayoutParams ----获取窗体属性
alpha—透明度
setAttributes----窗体消失恢复透明度
代码-设置动画:
<translate
android:duration="2000"
android:fromYDelta="0"
android:toYDelta="100%" />
<style name="MyPopupWindow">
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<item name="android:windowExitAnimation">@anim/pop_out</item>
</style>
setAnimationStyle----为菜单设置动画
自定义PopupWindow:
自己写类,对于PopupWindow继承
重写构造
setContentView----设置界面
setWidth setHeight ----设置高和宽
setAnimationStyle----设置动画
setFocusable----获取焦点
setTouchable----设置可以触摸
setOutsideTouchable----点击外部消失
自定义对话框
//控件
ImageView imageView;
TextView textView;
Button bt_no,bt_ok;
//数据
int img_id;
String message;
String ok_txt,no_txt;
//监听
OkListener okListener;
public void setOkListener(OkListener okListener) {
this.okListener = okListener;
}
public void setImg_id(int img_id) {
this.img_id = img_id;
}
public void setMessage(String message) {
this.message = message;
}
public void setOk_txt(String ok_txt) {
this.ok_txt = ok_txt;
}
public void setNo_txt(String no_txt) {
this.no_txt = no_txt;
}
public DiyDialog(Context context) {
super(context);
}
public DiyDialog( Context context, int themeResId) {
super(context, themeResId);
}
protected DiyDialog( Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diy_dialog);
imageView=findViewById(R.id.iv);
textView=findViewById(R.id.tv);
bt_no=findViewById(R.id.bt_no);
bt_ok=findViewById(R.id.bt_ok);
imageView.setImageResource(img_id);
textView.setText(message);
bt_no.setText(no_txt);
bt_ok.setText(ok_txt);
bt_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
okListener.ok();
}
});
bt_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
首先继承Dialog
然后属性有控件, 数据, 监听器
重写onCreate
为控件赋予id
为控件填充内容
接口回调
diyDialog.setOkListener(new OkListener() {
@Override
public void ok() {
//做处理
Toast.makeText(DiyActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
diyDialog.dismiss();
}
});
首先创建监听器接口和抽象方法
在自定义对话框中写入监听器属性
需要在主界面回调
自己对于内容进行编辑