下面列出了android.view.Window#setLayout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
if (window != null) {
Display display = window.getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= 17) display.getRealMetrics(metrics);
else display.getMetrics(metrics);
int minSize = Math.min(metrics.widthPixels, metrics.heightPixels);
window.setLayout(minSize, -1);
if (Build.VERSION.SDK_INT >= 21) {
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(mWidget.getNavigationBarColor());
}
}
}
public AdvDialog(Context context) {
super(context, R.style.BaseDialog);
//初始化布局
setContentView(R.layout.vw_adv_dialog);
mImageView = (ImageView) findViewById(R.id.image_view);
Window dialogWindow = getWindow();
int width = ResHelper.getScreenWidth() * 560 / 720;
int height = ResHelper.getScreenHeight() * 620 / 1280;
ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
lp.width = width;
lp.height = height;
dialogWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialogWindow.setGravity(Gravity.CENTER);
setCanceledOnTouchOutside(true);
findViewById(R.id.btn_view).setOnClickListener(this);
mImageView.setOnClickListener(this);
}
public static BDialog newInstance(final @NonNull Context context,
final @NonNull CharSequence title,
final @NonNull CharSequence subText,
final @Nullable CharSequence[] options,
final @Nullable DialogBoxListener positive,
final @Nullable DialogBoxListener negative,
final int inputType,
final StartingIndexChooser startingIndexChooser,
final @Nullable View extraView,
final @Nullable CharSequence negativeCustomText,
final @Nullable CharSequence positiveCustomText,
final int flags
) {
final BDialog baldDialogBox = new BDialog(context, title, subText, options, positive, negative, inputType, startingIndexChooser, extraView, negativeCustomText, positiveCustomText, flags);
baldDialogBox.show();
Window window = baldDialogBox.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // This flag is required to set otherwise the setDimAmount method will not show any effect
window.setDimAmount(DIM_LEVEL);
return baldDialogBox;
}
@Override
protected void onStart() {
super.onStart();
Window win = this.getWindow();
if (win == null) {
return;
}
win.getDecorView().setPadding(0, 0, 0, 0);
win.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL); //可设置dialog的位置
Resources resources = getContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
WindowManager.LayoutParams params = win.getAttributes();
if (mDialogHeight > 0) {
win.setLayout(dm.widthPixels, mDialogHeight);
}
else {
win.setLayout(dm.widthPixels, params.height);
}
}
@Override
public void onStart() {
super.onStart();
Window window = requireDialog().getWindow();
// Dialogs use a background with an InsetDrawable by default, so we have to replace it.
if (fullscreen) {
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
window.setBackgroundDrawable(background);
} else {
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int inset =
getResources().getDimensionPixelOffset(R.dimen.mtrl_calendar_dialog_background_inset);
Rect insets = new Rect(inset, inset, inset, inset);
window.setBackgroundDrawable(new InsetDrawable(background, inset, inset, inset, inset));
window
.getDecorView()
.setOnTouchListener(new InsetDialogOnTouchListener(requireDialog(), insets));
}
startPickerFragment();
}
BiometricPromptCompatDialog(@NonNull Context context) {
super(context, findThemeResId(context));
setCancelable(true);
setCanceledOnTouchOutside(true);
Window window = Objects.requireNonNull(getWindow());
window.setLayout(MATCH_PARENT, WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
View rootView = LayoutInflater.from(getContext())
.inflate(R.layout.biometric_prompt_dialog_content, null);
mSubtitle = rootView.findViewById(R.id.subtitle);
mDescription = rootView.findViewById(R.id.description);
mStatus = rootView.findViewById(R.id.status);
mNegativeButton = rootView.findViewById(android.R.id.button1);
mFingerprintIcon = rootView.findViewById(R.id.fingerprint_icon);
addContentView(rootView, new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
}
/**
* Show dialog in full width / show keyboard
*
* @param dialog Get via dialog.show()
*/
public void dialogFullWidth(AlertDialog dialog, boolean fullWidth, boolean showKeyboard) {
try {
Window w;
if (dialog != null && (w = dialog.getWindow()) != null) {
if (fullWidth) {
w.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}
if (showKeyboard) {
w.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
} catch (Exception ignored) {
}
}
public SelectPhotoDialog(Context context) {
super(context, R.style.SelectPhotoDialogStyle);
//初始化布局
setContentView(R.layout.vw_select_camera_dialog);
ButterKnife.inject(this);
Window dialogWindow = getWindow();
dialogWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialogWindow.setGravity(Gravity.BOTTOM);
setCanceledOnTouchOutside(true);
}
public DialogUtil create() {
View layout = ((LayoutInflater) this.mContext.getSystemService("layout_inflater")).inflate(R.layout.x9_screen_dialog_double_button, null);
TextView textView = (TextView) layout.findViewById(R.id.progress_tv);
ImageView imageView = (ImageView) layout.findViewById(R.id.img_show);
ProgressBar progressBar = (ProgressBar) layout.findViewById(R.id.progressBar);
if (this.isShowImage) {
imageView.setVisibility(0);
progressBar.setVisibility(4);
} else {
imageView.setVisibility(8);
progressBar.setVisibility(0);
}
if (this.message != null) {
textView.setText(this.message);
}
this.dialog.setContentView(layout);
this.dialog.getWindow().setGravity(17);
this.dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
Window window = this.dialog.getWindow();
window.setLayout((int) this.mContext.getResources().getDimension(R.dimen.dialog_width), -2);
Window dialogWindow = this.dialog.getWindow();
LayoutParams lp = dialogWindow.getAttributes();
lp.alpha = 0.9f;
window.setAttributes(lp);
lp.height = (int) ((218.0f * (AbViewUtil.getScreenHeight(this.mContext) > AbViewUtil.getScreenWidth(this.mContext) ? (float) AbViewUtil.getScreenHeight(this.mContext) : (float) AbViewUtil.getScreenWidth(this.mContext))) / 1920.0f);
dialogWindow.setAttributes(lp);
this.dialog.getWindow().getDecorView().setBackgroundColor(0);
return this.dialog;
}
@Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d != null) {
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.MATCH_PARENT;
Window window = d.getWindow();
if (window != null) {
window.setLayout(width, height);
window.setWindowAnimations(R.style.alertDialogAnimation);
}
}
}
public void setColorsAndSize() {
dialogs = new GlobalColorSizeDialog(this, mAppsList);
Window window = dialogs.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawableResource(android.R.color.transparent);
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
dialogs.show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.folderchooser_dialog, null);
Window window = getDialog().getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
//window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//window.setGravity(Gravity.TOP | Gravity.LEFT);
// retrieve display dimensions
Rect displayRectangle = new Rect();
window.getDecorView().getWindowVisibleDisplayFrame(displayRectangle);
int w = (int) (displayRectangle.width() * 0.95);
int h = (int) (displayRectangle.height() * 0.9);
window.setLayout(w, h);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
//params.x = 280;
//params.y = 280;
//params.height = 200;
//params.dimAmount = 0f;
window.setAttributes(params);
return view;
}
@Override
public void onResume() {
super.onResume();
// Set dialog size
Dialog dialog = getDialog();
if (dialog != null) {
Window window = dialog.getWindow();
if (window != null) {
int width = getResources().getDimensionPixelSize(R.dimen.imagetransfer_resolutiondialog_width);
int height = getResources().getDimensionPixelSize(R.dimen.imagetransfer_resolutiondialog_height);
window.setLayout(width, height);
}
}
}
private void showDialogForView(View view) {
mDialog = new Dialog(mActivity) {
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!mIgnorePendingWindowFocusChangeForClose && !hasFocus) super.dismiss();
setIgnorePendingWindowFocusChangeForClose(false);
}
};
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setCanceledOnTouchOutside(true);
mDialog.addContentView(view,
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mItemSelectedCallback.onItemSelected("");
}
});
Window window = mDialog.getWindow();
if (!DeviceFormFactor.isTablet()) {
// On smaller screens, make the dialog fill the width of the screen,
// and appear at the top.
window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
window.setGravity(Gravity.TOP);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
mDialog.show();
}
@Override
public void onResume() {
super.onResume();
Window window = getDialog().getWindow();
//设置dialog宽度为屏幕75%,高度随内容扩充
if (window != null) {
window.setLayout(getDialogWidth(), getDialogHeight());
//去掉dialog原有白色背景
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setGravity(gravity());
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dialog_bottom);
Window window = getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
layoutParams.dimAmount = 0.5f;
window.setGravity(Gravity.BOTTOM);
window.setAttributes(layoutParams);
window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
photographBtn = (Button) findViewById(R.id.photographBtn);
localPhotosBtn = (Button) findViewById(R.id.localPhotosBtn);
cancelBtn = (Button) findViewById(R.id.cancelBtn);
if (!TextUtils.isEmpty(confirmText)) {
photographBtn.setText(confirmText);
}
if (!TextUtils.isEmpty(middleText)) {
localPhotosBtn.setText(middleText);
}
if (!TextUtils.isEmpty(cancelText)) {
cancelBtn.setText(cancelText);
}
cancelBtn.setOnClickListener(this);
photographBtn.setOnClickListener(this);
localPhotosBtn.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
Window window = dialog == null ? null : dialog.getWindow();
if (null != dialog && null != window) {
window.setLayout(-1, -2);
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (mBubbleLayout == null)
{
mBubbleLayout = new BubbleLayout(getContext());
}
if (mAddView != null)
{
mBubbleLayout.addView(mAddView);
}
setContentView(mBubbleLayout);
final Window window = getWindow();
if (window == null) return;
if (mSoftShowUp)
{
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
onAutoPosition();
setLook();
// mBubbleLayout.post(new Runnable()
// {
// @Override
// public void run()
// {
// dialogPosition();
// }
// });
mBubbleLayout.measure(0, 0);
dialogPosition();
mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
{
int lastWidth, lastHeight;
@Override
public void onGlobalLayout()
{
if (lastWidth == mBubbleLayout.getMeasuredWidth() && lastHeight == mBubbleLayout.getMeasuredHeight()) return;
dialogPosition();
lastWidth = mBubbleLayout.getMeasuredWidth();
lastHeight = mBubbleLayout.getMeasuredHeight();
}
};
mBubbleLayout.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
mBubbleLayout.setOnClickEdgeListener(new BubbleLayout.OnClickEdgeListener()
{
@Override
public void edge()
{
if (BubbleDialog.this.mCancelable)
{
dismiss();
}
}
});
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// init dependency injection
retryCount = 0;
mActivity = getActivity();
mTargetFragment = getTargetFragment();
if (mActivity instanceof ActivityWithComponent) {
((ActivityWithComponent) mActivity).getActivityComponent().inject(this);
}
String destination = getArguments().getString("destination");
float sendAmountF = Float.parseFloat(getArguments().getString("amount"));
String amount;
if (sendAmountF != 0) {
maxSend = false;
amount = String.format(Locale.ENGLISH, "%.6f", sendAmountF);
} else {
maxSend = true;
amount = wallet.getAccountBalanceBananoNoComma();
}
boolean useLocalCurrency = getArguments().getBoolean("useLocalCurrency", false);
// subscribe to bus
RxBus.get().register(this);
// get address
Contact contact = null;
if (destination.startsWith("@")) {
contact = realm.where(Contact.class).equalTo("name", destination).findFirst();
if (contact == null) {
if (mTargetFragment != null) {
mTargetFragment.onActivityResult(getTargetRequestCode(), SEND_FAILED, mActivity.getIntent());
}
dismiss();
}
}
if (contact != null) {
address = new Address(contact.getAddress());
} else {
address = new Address(destination);
}
// Set send amount
wallet.setSendNanoAmount(amount);
// inflate the view
binding = DataBindingUtil.inflate(
inflater, R.layout.fragment_send_confirm, container, false);
view = binding.getRoot();
binding.setHandlers(new ClickHandlers());
// Restrict height
Window window = getDialog().getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, UIUtil.getDialogHeight(false, getContext()));
window.setGravity(Gravity.BOTTOM);
// colorize address text
if (binding != null &&
binding.sendDestination != null &&
address != null &&
address.getAddress() != null) {
if (contact != null) {
String prependString = contact.getName() + "\n";
binding.sendDestination.setText(UIUtil.getColorizedSpannableBrightPrepend(prependString, address.getAddress(), getContext()));
} else {
binding.sendDestination.setText(UIUtil.getColorizedSpannableBright(address.getAddress(), getContext()));
}
}
if (!useLocalCurrency) {
binding.sendAmount.setText(String.format("%s NANO", amount));
} else {
binding.sendAmount.setText(String.format("%s NANO (%s)", amount, wallet.getLocalCurrencyAmount()));
}
// Lottie hardware acceleration
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
binding.animationView.useHardwareAcceleration(true);
}
return view;
}
protected void onDialogAttachWindow(@NonNull Window window) {
window.setGravity(Gravity.CENTER);
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}