下面列出了android.view.WindowManager#LayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public WindowManager.LayoutParams creatParams(int type, FloatParams floatParams) {
//系统浮窗不能超出边界,更新xy
int ww = (w - floatParams.w) / 2;
int hh = (h - floatParams.h) / 2;
if (Math.abs(floatParams.x) > ww)
floatParams.x = floatParams.x > 0 ? ww : -ww;
if (Math.abs(floatParams.y) > hh)
floatParams.y = floatParams.y > 0 ? hh : -hh;
LayoutParams smallWindowParams = new LayoutParams();
smallWindowParams.type = type;
smallWindowParams.format = PixelFormat.RGBA_8888;
smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
//smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
smallWindowParams.width = floatParams.w;
smallWindowParams.height = floatParams.h;
smallWindowParams.x = floatParams.x;
smallWindowParams.y = floatParams.y;
return smallWindowParams;
}
protected void updateHeaderViewPosition(View headerView) {
// Refresh the Display Rect of the Decor View
mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(mRect);
WindowManager.LayoutParams wlp = null;
if (headerView.getLayoutParams() instanceof WindowManager.LayoutParams) {
wlp = (WindowManager.LayoutParams) headerView.getLayoutParams();
} else if (headerView.getTag() instanceof WindowManager.LayoutParams) {
wlp = (WindowManager.LayoutParams) headerView.getTag();
}
if (wlp != null && wlp.y != mRect.top) {
wlp.y = mRect.top;
mActivity.getWindowManager().updateViewLayout(headerView, wlp);
}
}
/**
* 显示小悬浮窗
*/
public void showSmallFloatWin() {
if (DEBUG) {
LogX.d(TAG, SUB_TAG, "showSmallFloatWin:创建悬浮窗口");
}
if (smallView == null) {
smallView = new SmallFloatWindowView(Manager.getContext(), dm.density);
smallParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
FloatWindowUtils.getType(),
FLAG_NOT_FOCUSABLE | FLAG_ALT_FOCUSABLE_IM, PixelFormat.TRANSLUCENT);
smallParams.gravity = Gravity.LEFT | Gravity.TOP;
smallParams.x = (int) xPosition;
smallParams.y = (int) (dm.heightPixels / 4 - yPosition);
smallView.setWindowsParams(smallParams);
smallView.setOnSmallCallback(this);
}
removeOldFloatWindow();
getWindowManager().addView(smallView, smallParams);
currentState = FloatWindowState.SMALL_WINDOW;
}
/**
* @param lightness 0 - 200
*/
private void setScreenLightness(boolean enable, int lightness) {
if (null == mMaskView) {
return;
}
Window w = getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
if (enable) {
lightness = MathUtils.clamp(lightness, 0, 200);
if (lightness > 100) {
mMaskView.setColor(0);
// Avoid BRIGHTNESS_OVERRIDE_OFF,
// screen may be off when lp.screenBrightness is 0.0f
lp.screenBrightness = Math.max((lightness - 100) / 100.0f, 0.01f);
} else {
mMaskView.setColor(MathUtils.lerp(0xde, 0x00, lightness / 100.0f) << 24);
lp.screenBrightness = 0.01f;
}
} else {
mMaskView.setColor(0);
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
}
w.setAttributes(lp);
}
/**
* Displays the content view in a popup window anchored to the corner of
* another view. The window is positioned according to the specified
* gravity and offset by the specified x and y coordinates.
* <p>
* If there is not enough room on screen to show the popup in its entirety,
* this method tries to find a parent scroll view to scroll. If no parent
* view can be scrolled, the specified vertical gravity will be ignored and
* the popup will anchor itself such that it is visible.
* <p>
* If the view later scrolls to move <code>anchor</code> to a different
* location, the popup will be moved correspondingly.
*
* @param anchor the view on which to pin the popup window
* @param xoff A horizontal offset from the anchor in pixels
* @param yoff A vertical offset from the anchor in pixels
* @param gravity Alignment of the popup relative to the anchor
*
* @see #dismiss()
*/
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
if (isShowing() || !hasContentView()) {
return;
}
TransitionManager.endTransitions(mDecorView);
attachToAnchor(anchor, xoff, yoff, gravity);
mIsShowing = true;
mIsDropdown = true;
final WindowManager.LayoutParams p =
createPopupLayoutParams(anchor.getApplicationWindowToken());
preparePopup(p);
final boolean aboveAnchor = findDropDownPosition(anchor, p, xoff, yoff,
p.width, p.height, gravity, mAllowScrollingAnchorParent);
updateAboveAnchor(aboveAnchor);
p.accessibilityIdOfAnchor = (anchor != null) ? anchor.getAccessibilityViewId() : -1;
invokePopup(p);
}
public PopupMenuDialog builder() {
View view = LayoutInflater.from(context).inflate(
R.layout.layout_popup_menu_dialog, null);
view.setMinimumWidth(display.getWidth());
dialog = new Dialog(context, R.style.PopupMenuDialogStyle);
dialog.setContentView(view);
mUnbinder = ButterKnife.bind(this, dialog);
dialog.setOnDismissListener(this::onDialogDismiss);
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.x = 0;
lp.y = 0;
dialogWindow.setAttributes(lp);
return this;
}
public UserSwitchingDialog(ActivityManagerService service, Context context, UserInfo oldUser,
UserInfo newUser, boolean aboveSystem, String switchingFromSystemUserMessage,
String switchingToSystemUserMessage) {
super(context);
mContext = context;
mService = service;
mUserId = newUser.id;
mOldUser = oldUser;
mNewUser = newUser;
mSwitchingFromSystemUserMessage = switchingFromSystemUserMessage;
mSwitchingToSystemUserMessage = switchingToSystemUserMessage;
inflateContent();
if (aboveSystem) {
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
}
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SYSTEM_ERROR |
WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
getWindow().setAttributes(attrs);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.global_dialog_progress_loading);
if (mAutoWidth)
{
setMinimumWidth(0);
}
mLoadingAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.progress_dialog_loading);
mImageView = (ImageView) findViewById(R.id.image);
mTextView = (TextView) findViewById(R.id.text);
// mTextView.setGravity(Gravity.LEFT);
mTextView.setText("");
Window dialogWindow = this.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
// lp.x = 100; // 新位置X坐标
// lp.y = 100; // 新位置Y坐标
lp.width = WindowManager.LayoutParams.WRAP_CONTENT; // 宽度
// lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度
lp.height = ViewUtils.dip2px(getContext(), 80); // 高度
lp.alpha = 1.0f; // 透明度
// 当Window的Attributes改变时系统会调用此函数,可以直接调用以应用上面对窗口参数的更改,也可以用setAttributes
// dialog.onWindowAttributesChanged(lp);
dialogWindow.setAttributes(lp);
}
/**
* Creates a {@link android.app.AlertDialog} with the arguments supplied
* to this builder and {@link Dialog#show()}'s the dialog.
*/
public MLAlertDialog show() {
MLAlertDialog dialog = create();
dialog.show();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = WindowManager.LayoutParams.FILL_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
return dialog;
}
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
WindowManager.LayoutParams WPARAMS = super.getWindowLayoutParams();
WPARAMS.flags &= ~WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
WPARAMS.flags &= ~WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
WPARAMS.y = getResources().getDimensionPixelSize(R.dimen.volume_panel_top);
return WPARAMS;
}
private boolean isReadyToRemove() {
WindowManager.LayoutParams removeParams = (WindowManager.LayoutParams) removeWidgetView.getLayoutParams();
removeBounds.set(removeParams.x, removeParams.y, removeParams.x + widgetHeight, removeParams.y + widgetHeight);
WindowManager.LayoutParams params = (WindowManager.LayoutParams) playPauseButton.getLayoutParams();
float cx = params.x + widgetHeight;
float cy = params.y + widgetHeight;
return removeBounds.contains(cx, cy);
}
/*****************************************************
* Overrided methods
******************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//----- Set title
if(mDialogTitle != null) {
setTitle(mDialogTitle);
} else {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
WindowManager.LayoutParams lpWindow = new WindowManager.LayoutParams();
lpWindow.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
lpWindow.dimAmount = 0.8f;
getWindow().setAttributes(lpWindow);
setContentView(R.layout.dialog_filter_list);
mClickListener = new OnClickListener(this);
mBtnDisablePackage = (Button) findViewById(R.id.btn_disable_package);
mBtnDisablePackage.setOnClickListener(mClickListener);
mBtnEdit = (Button) findViewById(R.id.btn_edit);
mBtnEdit.setOnClickListener(mClickListener);
setContent();
}
private void full(boolean enable) {
if (enable) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(lp);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
} else {
WindowManager.LayoutParams attr = getWindow().getAttributes();
attr.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attr);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
/**
* 设置状态栏颜色
*
* @param window
* @param color
*/
private static void setStatusBarColor(Window window, int color) {
WindowManager.LayoutParams winParams = window.getAttributes();
if (mStatusBarColorFiled != null) {
try {
int oldColor = mStatusBarColorFiled.getInt(winParams);
if (oldColor != color) {
mStatusBarColorFiled.set(winParams, color);
window.setAttributes(winParams);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* 设置窗口样式
*/
private void setWindow() {
//窗口对齐屏幕宽度
Window win = this.getWindow();
win.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams lp = win.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.BOTTOM;//设置对话框置顶显示
win.setAttributes(lp);
}
public String printStatus ()
{
WindowManager.LayoutParams layoutpars = MainActivity.mainActivity.getWindow().getAttributes();
float brightness = Math.abs(layoutpars.screenBrightness);
String result = String.format("%.1f%%", brightness * 100);
return result;
}
protected void showStatusBar() {
WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);
if(statusBarView != null){
statusBarView.setBackgroundColor(statusBarColor);
}
}
private void addOverlayView(OverlayView overlayView, WindowManager.LayoutParams layoutParams) {
mWm.addView(overlayView, layoutParams);
}
public void show(){
WindowManager.LayoutParams params = getWmParams();
this.windowManager.addView(this, params);
getContext().registerReceiver(dismissReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
private void screenOff() {
if (ActivityCallViewModel.isConnected) {
WindowManager.LayoutParams params = this.getWindow().getAttributes();
params.screenBrightness = 0;
this.getWindow().setAttributes(params);
enableDisableViewGroup((ViewGroup) activityCallBinding.acLayoutCallRoot, false);
}
}