popupwindow的位置安装有无偏移分,可以分为偏移和无偏移两种,
按照参照物的不同,可以分为相对于某个控件和相对于父控件,
具体如下,
showAsDropDown(View anchor):相对某个控件的位置(正左下方) 无偏移
showAsDropDown(View anchor,int xoff,intyoff):相对某个控件的位置,有偏移
showAtLocation(View parent,int gravity,int x,int y):相对于父控件的位置(例如Gravity.CENTER,Gravity.BOTTOM等),可以设置偏移或无偏移
//设置背景颜色
popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
//设置透明度0-255
popupWindow.getBackground().setAlpha(100);
//设置动画效果
popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
//设置点击窗口外边窗口消失
popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setTouchable(true);
//防止虚拟软键盘被弹出菜单遮住
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//获取屏幕和PopWindow的width和height
DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenHeight = dm.heightPixels; mScreenWidth = dm.widthPixels;
package com.example.application04;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
public class MainActivity_pw extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_pw);
}
//按钮事件方法
public void showWindow(View v){
View view = getLayoutInflater().inflate(R.layout.popup_window_layout,null);
//创建popupWindow(窗体的视图,)
PopupWindow popupWindow = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//设置背景颜色
popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default));
//设置动画效果
popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);
//设置透明度0-255
popupWindow.getBackground().setAlpha(100);
//设置点击窗口外边窗口消失
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
//防止虚拟软键盘被弹出菜单遮住
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//显示弹窗
popupWindow.showAtLocation(v, Gravity.BOTTOM,0,0);
}
}