下面列出了android.view.Window#setBackgroundDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showDialogForView(View view) {
mDialog = new Dialog(mActivity);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
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(mActivity)) {
// 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 onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
Window window = dialog.getWindow();
if (window != null) {
// remove background
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
// remove dim
WindowManager.LayoutParams windowParams = window.getAttributes();
window.setDimAmount(0.0F);
window.setAttributes(windowParams);
}
}
}
private void initDialog() {
contentLayout = new FrameLayout(activity);
contentLayout.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
contentLayout.setFocusable(true);
contentLayout.setFocusableInTouchMode(true);
dialog = new Dialog(activity);
dialog.setCanceledOnTouchOutside(true);//触摸屏幕取消窗体
dialog.setCancelable(true);//按返回键取消窗体
dialog.setOnKeyListener(this);
dialog.setOnDismissListener(this);
Window window = dialog.getWindow();
if (window != null) {
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//AndroidRuntimeException: requestFeature() must be called before adding content
window.requestFeature(Window.FEATURE_NO_TITLE);
window.setContentView(contentLayout);
}
setSize(screenWidthPixels, WRAP_CONTENT);
}
/**
* Clip given window to round.
*
* This method should be called before window is show.
*
* @param window window needs to be clipped.
* @return If clip succeed.
*/
public static boolean clipToScreenShape(final Window window) {
if (window == null || window.getDecorView() == null) {
return false;
}
// Record original drawable & set window to transparent to avoid window has solid color.
final Drawable original = window.getDecorView().getBackground();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.getDecorView().setOnApplyWindowInsetsListener(new OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
// Delay setting window background when shape is defined.
Drawable background = getBackgroundDrawable(original, insets.isRound());
window.setBackgroundDrawable(background);
if (insets.isRound()) {
clipToRound(v);
}
return insets;
}
});
window.getDecorView().requestApplyInsets();
return true;
}
protected BuildBean buildCenterSheet(BuildBean bean) {
AlertDialog.Builder builder = new AlertDialog.Builder(bean.mContext);
SheetHolder holder = new SheetHolder(bean.mContext);
builder.setView(holder.rootView);
AlertDialog dialog = builder.create();
bean.alertDialog = dialog;
if (bean.isVertical && !TextUtils.isEmpty(bean.bottomTxt)) {
Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
holder.assingDatasAndEvents(bean.mContext, bean);
return bean;
}
public static void setDialogStyle(Context context, Dialog dialog, int measuredHeight, BuildBean bean) {
if (dialog == null) {
return;
}
Window window = dialog.getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setGravity(bean.gravity);
WindowManager.LayoutParams wl = window.getAttributes();
// 以下这两句是为了保证按钮可以水平满屏
int width = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
int height = (int) (((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight() * 0.9);
if (bean.type != CommonConfig.TYPE_MD_LOADING_VERTICAL) {
wl.width = (int) (width * 0.94); // todo keycode to keep gap
} else {
wl.width = ViewGroup.LayoutParams.WRAP_CONTENT;
}
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT; //TODO 一般情况下为wrapcontent,最大值为height*0.9
if (measuredHeight > height) {
wl.height = height;
}
if (context instanceof Activity) {
Activity activity1 = (Activity) context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
if (activity1.isDestroyed()) {
context = DialogUIUtils.appContext;
}
}
} else {
wl.type = WindowManager.LayoutParams.TYPE_TOAST;
//todo keycode to improve window level,同时要让它的后面半透明背景也拦截事件,不要传递到下面去
//todo 单例化,不然连续弹出两次,只能关掉第二次的
}
dialog.onWindowAttributesChanged(wl);
}
private static void setDialogStyle(Context activity, Dialog dialog,int measuredHeight ) {
Window window = dialog.getWindow();
//window.setWindowAnimations(R.style.dialog_center);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//todo keycode to show round corner
WindowManager.LayoutParams wl = window.getAttributes();
/* wl.x = 0;
wl.y = getWindowManager().getDefaultDisplay().getHeight();*/
// 以下这两句是为了保证按钮可以水平满屏
int width = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
int height = (int) (((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight() * 0.9);
// wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
wl.width = (int) (width * 0.94); // todo keycode to keep gap
wl.height = ViewGroup.LayoutParams.WRAP_CONTENT; //TODO 一般情况下为wrapcontent,最大值为height*0.9
/* ViewUtils.measureView(contentView);
int meHeight = contentView.getMeasuredHeight();//height 为0,weight为1时,控件计算所得height就是0
View textview = contentView.findViewById(R.id.tv_msg);
ViewUtils.measureView(textview);
int textHeight = textview.getMeasuredHeight();*/
if (measuredHeight > height){
wl.height = height;
}
//wl.horizontalMargin= 0.2f;
// 设置显示位置
// wl.gravity = Gravity.CENTER_HORIZONTAL;
if (!(activity instanceof Activity)){
wl.type = WindowManager.LayoutParams.TYPE_TOAST;//todo keycode to improve window level
}
dialog.onWindowAttributesChanged(wl);
}
@NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCancelable(false);
setCancelable(false);
Window window = dialog.getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setDimAmount(0);
}
return dialog;
}
private void attachToContent(Activity activity, KugouLayout kugouLayout){
Window window = activity.getWindow();
ViewGroup decorView = (ViewGroup)window.getDecorView();
ViewGroup decorChild= (ViewGroup)decorView.getChildAt(0);
decorView.removeAllViews();
mKugouLayout.mContentContainer.addView(decorChild, decorChild.getLayoutParams());
decorView.addView(mKugouLayout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
window.setBackgroundDrawable(new ColorDrawable(0x0));
}
private void initWindowView() {
Window window = getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
window.getDecorView().setBackground(null);
} else {
window.getDecorView().setBackgroundDrawable(null);
}
}
@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);
}
}
@NonNull
@Override
public final Dialog onCreateDialog(@Nullable Bundle bundle) {
Dialog dialog = super.onCreateDialog(bundle);
Context context = dialog.getContext();
int surfaceColor =
MaterialAttributes.resolveOrThrow(
context, R.attr.colorSurface, TimePickerDialog.class.getCanonicalName());
MaterialShapeDrawable background =
new MaterialShapeDrawable(
context,
null,
0,
R.style.Widget_MaterialComponents_TimePicker);
background.initializeElevationOverlay(context);
background.setFillColor(ColorStateList.valueOf(surfaceColor));
Window window = dialog.getWindow();
window.setBackgroundDrawable(background);
window.requestFeature(Window.FEATURE_NO_TITLE);
// On some Android APIs the dialog won't wrap content by default. Explicitly update here.
window.setLayout(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
return dialog;
}
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();
}
@Override
public void show() {
Window dialogWindow = getWindow();
dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
startAnimation(true);
super.show();
}
@Override
public void onStart() {
super.onStart();
Window window = getDialog().getWindow();
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = ScreenUtil.dp2px(mActivity, 240);
window.setAttributes(params);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
private void init(Context context) {
Window window =getWindow();
if (window != null) {
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
setContentView(R.layout.dialog_loading);
ContentLoadingProgressBar pb = findViewById(R.id.dialogLoadingPb);
pb.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(context, R.color.colorPrimary),
PorterDuff.Mode.MULTIPLY);
mMsgTv = findViewById(R.id.dialogLoadingMsgTv);
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog == null) return;
Window window = dialog.getWindow();
window.setLayout(MATCH_PARENT, WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
@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());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Defaults.setupInitialValues(getApplicationContext(), getIntent());
if (!Defaults.ACTIVITY_THEME.isEmpty()) {
setTheme(Utils.getR("style." + Defaults.ACTIVITY_THEME));
}
setupIds();
setContentView(main_layout_id);
isMultipleSelection = (1 != Defaults.MAX_IMAGE_SELECTION);
isShapeCircle = Defaults.SHAPE_CIRCLE == Defaults.SHAPE;
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (!Defaults.STATUS_BAR_COLOR.isEmpty()) {
window.setStatusBarColor(TiConvert.toColor(Defaults.STATUS_BAR_COLOR));
}
window.setBackgroundDrawable(TiConvert.toColorDrawable(Defaults.BACKGROUND_COLOR));
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
if (!Defaults.BAR_COLOR.isEmpty()) {
actionBar.setBackgroundDrawable(TiConvert.toColorDrawable(Defaults.BAR_COLOR));
}
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(Defaults.TITLE);
} else {
Log.e(TAG, Defaults.ACTION_BAR_ERROR_MSG);
}
mRecyclerView = new RecyclerView(TiApplication.getInstance());
mRecyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mRecyclerView.setLayoutManager(new GridLayoutManager(ImagePickerActivity.this, Defaults.GRID_SIZE));
FrameLayout frame_container = (FrameLayout) findViewById(container);
frame_container.addView(mRecyclerView);
frame_container.setBackgroundColor(TiConvert.toColor(Defaults.BACKGROUND_COLOR));
adapterSet = new PhotoAdapter(adapter);
mRecyclerView.setAdapter(adapterSet);
if ( (1 == Defaults.SHOW_DIVIDER) && (!isShapeCircle) ) {
mRecyclerView.addItemDecoration(new DividerDecoration());
}
setupGlideOptions(); // set glide-options to apply on image
// Get gallery photos in a new UI thread like AsyncTask to update UI changes properly
new FetchImages().execute();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
imagesAdapter = intent.getExtras().getParcelableArrayList(Defaults.Params.IMAGES);
Defaults.setupInitialValues(getApplicationContext(), intent);
if (!Defaults.ACTIVITY_THEME.isEmpty()) {
setTheme(Utils.getR("style." + Defaults.ACTIVITY_THEME));
}
setupIds();
setContentView(frame_layout);
isShapeCircle = Defaults.SHAPE_CIRCLE == Defaults.SHAPE;
if (Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (!Defaults.STATUS_BAR_COLOR.isEmpty()) {
window.setStatusBarColor(TiConvert.toColor(Defaults.STATUS_BAR_COLOR));
}
window.setBackgroundDrawable(TiConvert.toColorDrawable(Defaults.BACKGROUND_COLOR));
}
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
if (!Defaults.BAR_COLOR.isEmpty()) {
actionBar.setBackgroundDrawable(TiConvert.toColorDrawable(Defaults.BAR_COLOR));
}
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(Defaults.TITLE);
} else {
Log.e(TAG, Defaults.ACTION_BAR_ERROR_MSG);
}
mRecyclerView = new RecyclerView(TiApplication.getInstance());
mRecyclerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mRecyclerView.setLayoutManager(new GridLayoutManager(ImageViewerActivity.this, Defaults.GRID_SIZE));
FrameLayout frame_container = (FrameLayout) findViewById(frame_layout_id);
frame_container.addView(mRecyclerView);
frame_container.setBackgroundColor(TiConvert.toColor(Defaults.BACKGROUND_COLOR));
adapterSet = new PhotoAdapter(imagesAdapter);
mRecyclerView.setAdapter(adapterSet);
if ( (1 == Defaults.SHOW_DIVIDER) && (!isShapeCircle) ) {
mRecyclerView.addItemDecoration(new DividerDecoration());
}
setupGlideOptions(); // set glide-options to apply on image
}