下面列出了android.view.Window#setGravity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initWindow(double percent, boolean isFromBottom) {
if (!isFromBottom) {
rootLayout.setBackgroundResource(R.drawable.dialog_common_center_bg);
} else {
rootLayout.setBackgroundResource(R.drawable.dialog_common_bottom_bg);
}
Window window = getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = (int) (ScreenUtils.getScreenWidth(activity) * percent);
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
if (isFromBottom) {
window.setGravity(Gravity.BOTTOM);
}
window.setAttributes(lp);
}
public static Dialog showBottomItemDialog(Context context,
List<String> words,String bottomTxt,
boolean outsideCancleable, boolean cancleable,
final MyItemDialogListener listener){
Dialog dialog = buildDialog(context,cancleable,outsideCancleable);
int measuredHeight = assignBottomListDialogView(context,dialog,words,bottomTxt,listener);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.mystyle);
setDialogStyle(context,dialog,measuredHeight);
dialog.show();
return dialog;
}
private void initView() {
Window window = getWindow();
window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
View child = getLayoutInflater().inflate(R.layout.layout_dialog_base, null, false);
setContentView(child);
mContainer = (FrameLayout) findViewById(R.id.fl_container);
mBtnPanel = (FrameLayout) findViewById(R.id.fl_btn_panel);
closeIV = (ImageView) findViewById(R.id.iv_close);
closeIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
DisplayMetrics d = mContext.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
lp.width = (int) (d.widthPixels * 0.8); // 宽度设置为屏幕的0.9
dialogWindow.setAttributes(lp);
setIsCancelable(true);
setOnDismissListener(this);
}
public void createDialog() {
if (dialogView != null) {
mDialog = new Dialog(context, R.style.custom_dialog2);
mDialog.setCancelable(mPickerOptions.cancelable);//不能点外面取消,也不能点back取消
mDialog.setContentView(dialogView);
Window dialogWindow = mDialog.getWindow();
if (dialogWindow != null) {
dialogWindow.setWindowAnimations(R.style.picker_view_scale_anim);
dialogWindow.setGravity(Gravity.CENTER);//可以改成Bottom
}
mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (onDismissListener != null) {
onDismissListener.onDismiss(BasePickerView.this);
}
}
});
}
}
@Override
public void initView() {
activity = this;
Window window = getWindow();
window.setGravity(Gravity.LEFT | Gravity.TOP);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 0;
params.y = 0;
params.width = 1;
params.height = 1;
window.setAttributes(params);
receiver =new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("KeepLive");
registerReceiver(receiver, filter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.display_options_popup, container);
final ViewAnimator viewAnimator = v.findViewById(R.id.settings_view_container);
viewAnimator.setDisplayedChild(mCurrentView);
Window window = getDialog().getWindow();
if (window != null) {
window.setGravity(Gravity.TOP | Gravity.END);
// window.setDimAmount(0);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
return v;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(): savedInstanceState = [" + savedInstanceState + "]");
instance = new WeakReference<>(this);
Window window = getWindow();
window.setGravity(Gravity.TOP | Gravity.LEFT);
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.x = 0;
attributes.y = 0;
attributes.height = 1;
attributes.width = 1;
window.setAttributes(attributes);
}
@Override
public void onStart() {
super.onStart();
// without title and title divider
// Less dimmed background; see http://stackoverflow.com/q/13822842/56285
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
//CHECKSTYLE:OFF
params.dimAmount = getDimAmount(); // dim only a little bit
//CHECKSTYLE:ON
window.setAttributes(params);
window.setLayout(getWidth(), getHeight());
window.setGravity(getGravity());
// Transparent background; see http://stackoverflow.com/q/15007272/56285
// (Needed to make dialog's alpha shadow look good)
window.setBackgroundDrawableResource(android.R.color.transparent);
final Resources res = getResources();
final int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
if (titleDividerId > 0) {
final View titleDivider = getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(res.getColor(android.R.color.transparent));
}
}
}
public static Dialog infoPopupWithListner(Context context, String message, String buttonName, final View.OnClickListener listener) {
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().getAttributes().windowAnimations = R.style.animationdialog;
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView btnTxt = (TextView) dialog.findViewById(R.id.txtOK);
TextView txt = (TextView) dialog.findViewById(R.id.txt);
txt.setText(message);
btnTxt.setText(buttonName);
if (listener != null) {
btnTxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
listener.onClick(view);
}
});
}
dialog.show();
return dialog;
}
@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());
}
}
/**
* Creates a dialog window that uses a custom dialog style.
*
* @param context Context
* @param themeResId The dialog's layout resource
* @param isSetWin Set the gravity of the window
* @param gravity The desired gravity constant
* @param width The dialog's width
* @param height The dialog's height
*/
protected AbstractDialog(@NonNull Context context, @StyleRes int themeResId, boolean isSetWin,
int gravity, int width, int height) {
super(context, themeResId);
this.mContext = context;
this.mRootView = LayoutInflater.from(context).inflate(getLayoutRes(), null);
setContentView(this.mRootView);
setCanceledOnTouchOutside(true);
setCancelable(true);
if (isSetWin) {
Window dialogWindow = getWindow();
if (dialogWindow != null) {
dialogWindow.setWindowAnimations(-1);
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
dialogWindow.setGravity(gravity);
// Get the current layout param of the dialog
WindowManager.LayoutParams p = dialogWindow.getAttributes();
// Set dialog's width
p.width = width;
// Set dialog's height
p.height = height;
dialogWindow.setAttributes(p);
}
}
init(this.mRootView);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog == null) return;
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
Window window = dialog.getWindow();
window.setLayout(MATCH_PARENT, WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
/**
* 位于屏幕何处
*
* @see Gravity
*/
public void setGravity(int gravity) {
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(gravity);
}
if (gravity == Gravity.CENTER) {
//居于屏幕正中间时,宽度不允许填充屏幕
setWidth((int) (screenWidthPixels * 0.7f));
}
}
protected ConfigBean buildBottomItemDialog(ConfigBean bean){
IosActionSheetHolder holder = new IosActionSheetHolder(bean.context);
bean.viewHolder = holder;
bean.dialog.setContentView(holder.rootView);
holder.assingDatasAndEvents(bean.context,bean);
bean.viewHeight = Tool.mesureHeight(holder.rootView,holder.lv);
Window window = bean.dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.mystyle);
return bean;
}
@OnClick(R2.id.tv_profile_gender)
public void onViewClickedGender(View view) {
mGenderDialog.show();
final Window window = mGenderDialog.getWindow();
if (window != null) {
window.setContentView(R.layout.dialog_profile_sex);
window.setGravity(Gravity.BOTTOM);
if (gender.equals(male)) {
((RadioButton) window.findViewById(R.id.btn_dialog_profile_male)).setChecked(true);
} else {
((RadioButton) window.findViewById(R.id.btn_dialog_profile_female)).setChecked(true);
}
//设置弹出动画
window.setWindowAnimations(R.style.anim_panel_up_from_bottom);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//设置属性
final WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
//FLAG_DIM_BEHIND: 窗口之后的内容变暗 FLAG_BLUR_BEHIND: 窗口之后的内容变模糊。
params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(params);
((RadioButton) window.findViewById(R.id.btn_dialog_profile_male)).setOnCheckedChangeListener(this);
((RadioButton) window.findViewById(R.id.btn_dialog_profile_female)).setOnCheckedChangeListener(this);
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getDialog().getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (builder.gravity == Gravity.BOTTOM) window.setGravity(Gravity.BOTTOM);
View contentView = inflater.inflate(R.layout.dialog_wheel_picker_single, container, false);
return contentView;
}
/** show at location only valid for mIsPopupStyle true(指定位置显示,只对isPopupStyle为true有效) */
public void showAtLocation(int gravity, int x, int y) {
if (mIsPopupStyle) {
Window window = getWindow();
LayoutParams params = window.getAttributes();
window.setGravity(gravity);
params.x = x;
params.y = y;
}
show();
}
private void setDialogPosition(Dialog dialog) {
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(params);
}
/**
* 设置 Dialog 重心
*/
public void setGravity(int gravity) {
Window window = getWindow();
if (window != null) {
window.setGravity(gravity);
}
}
private void showDialog() {
// AlertDialog dialog = new AlertDialog(this);
AppCompatDialog dialog = new AppCompatDialog(this);
dialog.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//key code to remove title
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.mystyle);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//round corner
// window.setBackgroundDrawableResource(R.drawable.bg_ios_roundcorner);
// window.requestFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_ios_alert_bottom);
// AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 可以在此设置显示动画
WindowManager.LayoutParams wl = window.getAttributes();
/* wl.x = 0;
wl.y = getWindowManager().getDefaultDisplay().getHeight();*/
// 以下这两句是为了保证按钮可以水平满屏
int width = getWindowManager().getDefaultDisplay().getWidth();
// wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
wl.width = (int) (width * 0.85); // todo keycode gap
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//wl.horizontalMargin= 0.2f;
// 设置显示位置
// wl.gravity = Gravity.CENTER_HORIZONTAL;
dialog.onWindowAttributesChanged(wl);
dialog.show();
}