下面列出了怎么用android.view.WindowManager.LayoutParams的API类实例代码及写法,或者点击链接到github查看源代码。
public void addView(final View view) {
activity_.runOnUiThread(new Runnable() {
@Override
public void run() {
if (JUIRelativeLayout_ != null) {
JUIRelativeLayout_.addView(view);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view
.getLayoutParams();
if (params instanceof RelativeLayout.LayoutParams == false) {
// Switching to relative layout param
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
params.width, params.height);
layoutParams.leftMargin = params.leftMargin;
layoutParams.bottomMargin = params.bottomMargin;
layoutParams.rightMargin = params.rightMargin;
layoutParams.topMargin = params.topMargin;
view.setLayoutParams(layoutParams);
}
}
}
});
return;
}
@Override
protected void onStart() {
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
//System.out.println("d.getHeight():" + d.getHeight());
LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
p.height = (int) (d.getHeight() * 1.0); // 高度设置为屏幕的1.0
p.width = (int) (d.getWidth() * 1.0); // 宽度设置为屏幕的0.8
p.alpha = 1.0f; // 设置本身透明度
p.dimAmount = 0.0f; // 设置黑暗度
getWindow().setAttributes(p); // 设置生效
getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM); // 设置靠右对齐
super.onStart();
}
private static WindowManager.LayoutParams getWindowLayoutParams() {
int flags = (LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
LayoutParams.FLAG_DIM_BEHIND );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
flags |= LayoutParams.FLAG_HARDWARE_ACCELERATED;
if (!BuildConfig.DEBUG) flags |= LayoutParams.FLAG_SECURE;
LayoutParams WPARAMS = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_SYSTEM_ALERT, flags, PixelFormat.TRANSLUCENT);
final int windowAnimations = getInternalStyle("Animation_Dialog");
if (windowAnimations > 0) WPARAMS.windowAnimations = windowAnimations;
WPARAMS.dimAmount = 0.6f;
WPARAMS.packageName = PopupDialog.class.getPackage().getName();
WPARAMS.setTitle(TAG);
WPARAMS.gravity = Gravity.CENTER;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
WPARAMS.screenBrightness = WPARAMS.buttonBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
return WPARAMS;
}
public static int getSystemUiVisibility(WindowState win, LayoutParams attrs) {
attrs = attrs != null ? attrs : win.getAttrs();
int vis = win != null ? win.getSystemUiVisibility()
: (attrs.systemUiVisibility | attrs.subtreeSystemUiVisibility);
if (sImmersiveStatusFilter != null && sImmersiveStatusFilter.matches(attrs)) {
vis |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
vis &= ~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.STATUS_BAR_TRANSLUCENT);
}
if (sImmersiveNavigationFilter != null && sImmersiveNavigationFilter.matches(attrs)) {
vis |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
vis &= ~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.NAVIGATION_BAR_TRANSLUCENT);
}
return vis;
}
@Override
public void setValue(int level, boolean isTouch) {
if (isZero() && isTouch) {
level = historyLevel;
}
if (level < 0) {
level = 0;
}
else if (level > maxLevel) {
level = maxLevel;
}
float tempValue = level;
if (context != null && context instanceof Activity) {
LayoutParams lp = ((Activity) (context)).getWindow().getAttributes();
lp.screenBrightness = tempValue / maxLevel;
((Activity) (context)).getWindow().setAttributes(lp);
updateValue();
if (!isZero()) {
historyLevel = currentLevel;
}
if (changeListener != null) {
changeListener.onUpdateUI();
}
}
}
private void a() {
this.b = new TextView(this.a.F);
this.b.setTextColor(Color.rgb(255, 255, 255));
this.b.setTextSize(15.0f);
this.b.setShadowLayer(1.0f, 1.0f, 1.0f, Color.rgb(242, 211, 199));
this.b.setGravity(3);
this.b.setEllipsize(TruncateAt.END);
this.b.setIncludeFontPadding(false);
this.b.setSingleLine(true);
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, -2);
layoutParams.weight = 1.0f;
layoutParams.leftMargin = this.a.a(4);
addView(this.b, layoutParams);
this.c = new Button(this.a.F);
this.c.setPadding(0, 0, 0, 0);
this.c.setTextSize(16.0f);
this.c.setTextColor(Color.rgb(255, 255, 255));
this.c.setShadowLayer(1.0f, 1.0f, 1.0f, Color.rgb(242, 211, 199));
this.c.setIncludeFontPadding(false);
this.c.setOnClickListener(new f(this.a, this.d.a));
layoutParams = new LinearLayout.LayoutParams(this.a.a(TaskGuide.p), this.a.a(TaskGuide.q));
layoutParams.leftMargin = this.a.a(2);
layoutParams.rightMargin = this.a.a(8);
addView(this.c, layoutParams);
}
/**
* Wraps a content view in a PopupViewContainer.
*
* @param contentView the content view to wrap
* @return a PopupViewContainer that wraps the content view
*/
private PopupBackgroundView createBackgroundView(View contentView) {
final ViewGroup.LayoutParams layoutParams = mContentView.getLayoutParams();
final int height;
if (layoutParams != null && layoutParams.height == WRAP_CONTENT) {
height = WRAP_CONTENT;
} else {
height = MATCH_PARENT;
}
final PopupBackgroundView backgroundView = new PopupBackgroundView(mContext);
final PopupBackgroundView.LayoutParams listParams = new PopupBackgroundView.LayoutParams(
MATCH_PARENT, height);
backgroundView.addView(contentView, listParams);
return backgroundView;
}
/**
* <p>Invoke the popup window by adding the content view to the window
* manager.</p>
*
* <p>The content view must be non-null when this method is invoked.</p>
*
* @param p the layout parameters of the popup's content view
*/
private void invokePopup(WindowManager.LayoutParams p) {
if (mContext != null) {
p.packageName = mContext.getPackageName();
}
final PopupDecorView decorView = mDecorView;
decorView.setFitsSystemWindows(mLayoutInsetDecor);
setLayoutDirectionFromAnchor();
mWindowManager.addView(decorView, p);
if (mEnterTransition != null) {
decorView.requestEnterTransition(mEnterTransition);
}
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(LayoutParams.FLAG_NOT_TOUCHABLE);
CredentialRetrieveRequest request = getIntent().getParcelableExtra(EXTRA_REQUEST);
if (null == request) {
setResult(
CredentialRetrieveResult.CODE_UNKNOWN,
CredentialRetrieveResult.UNKNOWN.toResultDataIntent());
finish();
return;
}
BroadcastQueryClient.getInstance(this)
.queryFor(
CREDENTIAL_DATA_TYPE,
request.toProtocolBuffer(),
RETRIEVE_TIMEOUT_MS,
new CredentialRetrieveQueryCallback(request));
}
/**
* 对windowManager进行设置
*
* @param wmParams
* @return
*/
public static WindowManager.LayoutParams getParams(WindowManager.LayoutParams wmParams) {
wmParams = new WindowManager.LayoutParams();
// 设置window type 下面变量2002是在屏幕区域显示,2003则可以显示在状态栏之上
// wmParams.type = LayoutParams.TYPE_PHONE;
// wmParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
wmParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
// 设置图片格式,效果为背景透明
wmParams.format = PixelFormat.RGBA_8888;
// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
// wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
// 设置可以显示在状态栏上
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
// 设置悬浮窗口长宽数据
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
return wmParams;
}
public static AlertDialog buildBottomDialog(Activity activity, View contentView) {
AlertDialog dialog = new Builder(activity).create();
dialog.setCanceledOnTouchOutside(true);
dialog.show();
dialog.setContentView(contentView);
Window dialogWindow = dialog.getWindow();
Display d = activity.getWindowManager().getDefaultDisplay();
LayoutParams p = dialogWindow.getAttributes();
p.width = d.getWidth();
dialogWindow.setAttributes(p);
dialogWindow.setGravity(80);
return dialog;
}
public void setLayoutParams(final View view, final int width,
final int height) {
activity_.runOnUiThread(new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
}
});
}
private static void setParams(LayoutParams lay, Activity context)
{
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
Rect rect = new Rect();
View view = context.getWindow().getDecorView();
view.getWindowVisibleDisplayFrame(rect);
lay.height = dm.heightPixels - rect.top;
lay.width = dm.widthPixels;
}
/**
* Constructor
* @param rootView The top most layout in your view hierarchy. The difference of this view and the screen height will be used to calculate the keyboard height.
* @param mContext The context of current activity.
*/
public EmojiconsPopup(View rootView, Context mContext){
super(mContext);
this.mContext = mContext;
this.rootView = rootView;
View customView = createCustomView();
setContentView(customView);
setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//default size
setSize((int) mContext.getResources().getDimension(R.dimen.keyboard_height), LayoutParams.MATCH_PARENT);
}
public void setAnchorView(View view) {
if (this.mAnchor != null) {
this.mAnchor.removeOnLayoutChangeListener(this.mLayoutChangeListener);
}
this.mAnchor = view;
if (this.mAnchor != null) {
this.mAnchor.addOnLayoutChangeListener(this.mLayoutChangeListener);
}
FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(-1, -1);
removeAllViews();
addView(makeControllerView(), frameParams);
}
private void setBrightness(float value) {
if (value > 1.0f) {
value = 1.0f;
}
if (value < 0.1f) {
value = 0.1f;
}
Activity activity = this.mActivity;
LayoutParams wl = activity.getWindow().getAttributes();
wl.screenBrightness = value;
activity.getWindow().setAttributes(wl);
BaseApplication.getInstance().setBritness(value);
}
private static List<ViewRootData> viewRootData(Object[] roots, LayoutParams[] params) {
List<ViewRootData> rootViews = new ArrayList<>();
for (int i = 0; i < roots.length; i++) {
Object root = roots[i];
View rootView = (View) getFieldValue("mView", root);
// fixes https://github.com/jraska/Falcon/issues/10
if (rootView == null) {
Log.e(TAG, "null View stored as root in Global window manager, skipping");
continue;
}
if(!rootView.isShown()){
continue;
}
int[] location = new int[2];
rootView.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
Rect area = new Rect(left, top, left + rootView.getWidth(), top + rootView.getHeight());
rootViews.add(new ViewRootData(rootView, area, params[i]));
}
return rootViews;
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callback) throws JSONException {
// grab the correct methods
if(action.equalsIgnoreCase("enable")) {
if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
cordova.getActivity().runOnUiThread( new Runnable() {
public void run() {
cordova.getActivity().getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
});
callback.success();
} else {
callback.error("not supported");
}
return true;
} else if(action.equalsIgnoreCase("disable")) {
if(VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
cordova.getActivity().runOnUiThread( new Runnable() {
public void run() {
cordova.getActivity().getWindow().clearFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
});
callback.success();
} else {
callback.error("not supported");
}
return true;
} else {
callback.error("Unknown Action: " + action);
return false;
}
}
@Override
public int getSystemValueLevel() {
if (context != null && context instanceof Activity) {
LayoutParams lp = ((Activity) (context)).getWindow().getAttributes();
return lp.screenBrightness == -1 ? DEFAULT_MAX_BRIGHTNESS_VALUE : (int) (lp.screenBrightness * DEFAULT_MAX_BRIGHTNESS_VALUE);
}
return DEFAULT_MAX_BRIGHTNESS_VALUE;
}
private ViewGroup b(Context context) {
ViewGroup eVar = new e(this, context);
g[] gVarArr = this.h.c;
View iVar;
ViewGroup.LayoutParams layoutParams;
if (gVarArr.length == 1) {
iVar = new i(this, context, gVarArr[0]);
iVar.setId(1);
layoutParams = new RelativeLayout.LayoutParams(-1, -2);
layoutParams.addRule(15);
eVar.addView(iVar, layoutParams);
} else {
iVar = new i(this, context, gVarArr[0]);
iVar.setId(1);
View iVar2 = new i(this, context, gVarArr[1]);
iVar2.setId(2);
layoutParams = new RelativeLayout.LayoutParams(-1, -2);
layoutParams.addRule(14);
layoutParams.setMargins(0, a(6), 0, 0);
ViewGroup.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(-1, -2);
layoutParams2.addRule(14);
layoutParams2.setMargins(0, a(4), 0, 0);
layoutParams2.addRule(3, 1);
layoutParams2.addRule(5, 1);
eVar.addView(iVar, layoutParams);
eVar.addView(iVar2, layoutParams2);
}
eVar.setBackgroundDrawable(e());
return eVar;
}
/**
* Checks if the {@link Root} is ready. The UI is no longer in flux if layout of the root view is
* not being requested and the root view has window focus or is focusable.
*
* @return if the root view has focus
*/
public boolean isReady() {
if (!decorView.isLayoutRequested()) {
int flags = windowLayoutParams.get().flags;
return decorView.hasWindowFocus()
|| (flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
== WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
}
return false;
}
public EasyProgressDialog(Context context, int style, int layout) {
super(context, style);
mContext = context;
WindowManager.LayoutParams Params = getWindow().getAttributes();
Params.width = LayoutParams.FILL_PARENT;
Params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes(Params);
mLayoutId = layout;
}
public static int getWindowFlags(WindowState win, LayoutParams attrs) {
attrs = attrs != null ? attrs : win.getAttrs();
int flags = attrs.flags;
if (sImmersiveStatusFilter != null && sImmersiveStatusFilter.matches(attrs)) {
flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
flags &= ~(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
if (sImmersiveNavigationFilter != null && sImmersiveNavigationFilter.matches(attrs)) {
flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
}
return flags;
}
private void configureOverlayBeforeShow(SimpleOverlay overlay) {
/* The overlay covers the entire screen. However, there is a left, top, right, and
* bottom margin. */
final WindowManager.LayoutParams params = overlay.getParams();
final Point size = ScreenUtils.getRealScreenSize(getContext());
params.height = size.y;
params.width = size.x;
overlay.setParams(params);
}
/**
* Move the PopupWindow by a delta x and y. Pass 0 to keep the
* current position. <em>Note</em>: not all windows observe this behavior.
*/
public void move(final int dx, final int dy) {
LayoutParams wParams = getWindowParams();
wParams.gravity = (Gravity.LEFT | Gravity.TOP);
wParams.x += dx;
wParams.y += dy;
if (!mAllowOffScreen) bound();
onWindowAttributesChanged();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
windowManager = getWindowManager();
// 动态初始化图层
img = new ImageView(this);
img.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT));
img.setScaleType(ScaleType.FIT_XY);
img.setImageResource(R.drawable.guide);
// 设置LayoutParams参数
LayoutParams params = new WindowManager.LayoutParams();
// 设置显示的类型,TYPE_PHONE指的是来电话的时候会被覆盖,其他时候会在最前端,显示位置在stateBar下面,其他更多的值请查阅文档
params.type = WindowManager.LayoutParams.TYPE_PHONE;
// 设置显示格式
params.format = PixelFormat.RGBA_8888;
// 设置对齐方式
params.gravity = Gravity.LEFT | Gravity.TOP;
// 设置宽高
params.width = ScreenUtils.getScreenWidth(this);
params.height = ScreenUtils.getScreenHeight(this);
// 添加到当前的窗口上
windowManager.addView(img, params);
// 点击图层之后,将图层移除
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
windowManager.removeView(img);
}
});
}
@Override
protected boolean findDropDownPosition(View anchor, LayoutParams outParams,
int xOffset, int yOffset, int width, int height, int gravity, boolean allowScroll) {
if (mFullScreen) {
// In fullscreen mode, don't need consider the anchor view.
outParams.x = xOffset;
outParams.y = yOffset;
outParams.width = width;
outParams.height = height;
outParams.gravity = gravity;
return false;
}
return super.findDropDownPosition(anchor, outParams, xOffset, yOffset,
width, height, gravity, allowScroll);
}
public EasyAlertDialog(Context context, int resourceId, int style) {
super(context, style);
this.context = context;
if (-1 != resourceId) {
setContentView(resourceId);
this.resourceId = resourceId;
}
WindowManager.LayoutParams Params = getWindow().getAttributes();
Params.width = LayoutParams.MATCH_PARENT;
Params.height = LayoutParams.MATCH_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) Params);
}
@Override
public void onSensorChanged(SensorEvent event) {
WindowManager.LayoutParams params = this.getWindow().getAttributes();
if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
if (event.values[0] == 0) {
/* FileLog.d("onSensorChanged", "distance:" + event.values[0] + "");
params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = 0;
getWindow().setAttributes(params);*/
if (!mProximityWakeLock.isHeld()) {
mProximityWakeLock.acquire();
}
} else {
if (mProximityWakeLock.isHeld()) {
mProximityWakeLock.release();
}
/*params.flags |= LayoutParams.FLAG_KEEP_SCREEN_ON;
params.screenBrightness = -1f;
getWindow().setAttributes(params);*/
}
/*try {
// Yeah, this is hidden field.
//int field = PowerManager.class.getClass().getField("PROXIMITY_SCREEN_OFF_WAKE_LOCK").getInt(null);
} catch (Throwable ignored) {
}*/
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (context.getWindow().getAttributes().screenBrightness == 0.01f) {
LayoutParams params = context.getWindow().getAttributes();
params.screenBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
context.getWindow().setAttributes(params);
}
return super.dispatchTouchEvent(ev);
}