Android project construction
Display method | Display position |
---|---|
showAsDropDown(View anchor, int xoff, int yoff) | Displayed below the anchor control |
showAtLocation(View parent, int gravity, int x, int y) | Displayed in a certain position of the parent control |
1. The difference between popupwindow and dialog box
PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。
2. How to create a custom form
1、构造方法:public PopupWindow (Context context):context是一个上下文对象
2、必须设置的3大要素:
setContentView():设置布局
setWidth():设置宽度
setHeight():设置高度
3、显示窗体:
a.显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b.指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT
3. Effect
a. Specific operations and codes
步骤1.实例化PopupWindow对象
步骤2.设置自定义布局、宽度和高度
步骤3.指定位置显示: showAsDropDown() showAtLocation()
PopupWindow popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setWidth(300);//设置窗体的宽
popupWindow.setHeight(200);//设置窗体的高
popupWindow.setOutsideTouchable(true);//设置点击其他地方,窗体消失
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout, null);//找到一个布局文件
popupWindow.setContentView(view);//将布局设置上去
/**
* @param parent 父布局
* @param gravity gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT。。。。
* @param x x轴偏移量
* @param y y轴偏移量
*/
popupWindow.showAtLocation(view1, Gravity.BOTTOM,0,0);//设置在窗体的下方显示
//popupWindow.showAsDropDown(imageView); //在对应控件的下方显示
Set its transparency
WindowManager.LayoutParams attributes = getWindow().getAttributes();//获取窗体中的属性
attributes.alpha=0.5f;//将其背景设置半透明
getWindow().setAttributes(attributes);//修改完毕后,重新将属性设置回去
Form disappear event
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.alpha=1f;
getWindow().setAttributes(layoutParams);
}
});
3. Set up the animation
Create an anim folder and write animations in it
Set style in stylexml
Custom popupwindoe
public class MyPopupWindow extends PopupWindow {
Context mContext;
private LayoutInflater mInflater;
private View mContentView;
public MyPopupWindow(Context context) {
super(context);
this.mContext=context;
//打气筒
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//打气
mContentView = mInflater.inflate(R.layout.layout_dialog,null);
//设置View
setContentView(mContentView);
//设置宽与高
setWidth(WindowManager.LayoutParams.MATCH_PARENT);
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
/**
* 设置进出动画
*/
setAnimationStyle(R.style.MyPopupWindow);
/**
* 设置背景只有设置了这个才可以点击外边和BACK消失
*/
setBackgroundDrawable(new ColorDrawable());
/**
* 设置可以获取集点
*/
setFocusable(true);
/**
* 设置点击外边可以消失
*/
setOutsideTouchable(true);
/**
*设置可以触摸
*/
setTouchable(true);
/**
* 设置点击外部可以消失
*/
setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
/**
* 判断是不是点击了外部
*/
if(event.getAction()==MotionEvent.ACTION_OUTSIDE){
return true;
}
//不是点击外部
return false;
}
});
/**
* 初始化View与监听器
*/
initView();
initListener();
}
private void initView() {
}
private void initListener() {
}
}