下面列出了android.view.Window#setContentView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
final void beginCameraDialog(){
DIALOG.show();
final Window window = DIALOG.getWindow();
if(window != null){
window.setGravity(Gravity.BOTTOM);
window.setContentView(R.layout.dialog_camera_panel);
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;
params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
params.dimAmount = 0.5f;
window.setAttributes(params);
window.findViewById(R.id.photodialog_btn_cancel).setOnClickListener(this);
window.findViewById(R.id.photodialog_btn_take).setOnClickListener(this);
window.findViewById(R.id.photodialog_btn_native).setOnClickListener(this);
}
}
/**
* Show alertDialog
*
* @param mActivity activity instance
* @param content show content
* @param btnContent btn content
* @param onClickListener btn onClickListener
*/
public void showDialog(Activity mActivity, String content, String btnContent, final View.OnClickListener onClickListener) {
if (mActivity == null) {
return;
}
if (alertDialog == null) {
alertDialog = new AlertDialog.Builder(mActivity, R.style.myCorDialog).create();
}
if (!alertDialog.isShowing()) {
alertDialog.show();
}
alertDialog.setCanceledOnTouchOutside(false);
Window window = alertDialog.getWindow();
if (window != null) {
window.setContentView(R.layout.common_alert_dialog);
TextView contentTv = (TextView) window.findViewById(R.id.dialog_content);
contentTv.setText(content);
Button mOkBtn = (Button) window.findViewById(R.id.dialog_btn);
mOkBtn.setText(btnContent);
mOkBtn.setOnClickListener(onClickListener);
View btnDivider = window.findViewById(R.id.dialog_btn_divider);
btnDivider.setVisibility(View.GONE);
Button mNeBtn = (Button) window.findViewById(R.id.dialog_negative_btn);
mNeBtn.setVisibility(View.GONE);
}
}
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);
}
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);
}
@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);
}
}
/**
* Show alertDialog
*
* @param mActivity activity instance
* @param content show content
* @param btnContent btn content
*/
public void showDialog(Activity mActivity, String content, String btnContent) {
if (mActivity == null) {
return;
}
if (alertDialog == null) {
alertDialog = new AlertDialog.Builder(mActivity, R.style.myCorDialog).create();
}
if (!alertDialog.isShowing()) {
alertDialog.show();
}
alertDialog.setCanceledOnTouchOutside(false);
Window window = alertDialog.getWindow();
if (window != null) {
window.setContentView(R.layout.common_alert_dialog);
TextView contentTv = (TextView) window.findViewById(R.id.dialog_content);
contentTv.setText(content);
Button mOkBtn = (Button) window.findViewById(R.id.dialog_btn);
mOkBtn.setText(btnContent);
mOkBtn.setOnClickListener(v -> {
if (alertDialog != null) {
alertDialog.cancel();
alertDialog = null;
}
});
View btnDivider = window.findViewById(R.id.dialog_btn_divider);
btnDivider.setVisibility(View.GONE);
Button mNeBtn = (Button) window.findViewById(R.id.dialog_negative_btn);
mNeBtn.setVisibility(View.GONE);
}
}
/**
* Show alertDialog
*
* @param mActivity activity instance
* @param content show content
* @param btnContent ok btn content
* @param neContent negative btn content
* @param onPoClickListener ok btn onClickListener
* @param onNeClickListener negative btn onClickListener
*/
public void showDialog(Activity mActivity, String content, String btnContent, String neContent,
final View.OnClickListener onPoClickListener,
final View.OnClickListener onNeClickListener) {
if (mActivity == null) {
return;
}
if (alertDialog == null) {
alertDialog = new AlertDialog.Builder(mActivity, R.style.myCorDialog).create();
}
if (!alertDialog.isShowing()) {
alertDialog.show();
}
alertDialog.setCanceledOnTouchOutside(false);
Window window = alertDialog.getWindow();
if (window != null) {
window.setContentView(R.layout.common_alert_dialog);
TextView contentTv = (TextView) window.findViewById(R.id.dialog_content);
contentTv.setText(content);
Button mOkBtn = (Button) window.findViewById(R.id.dialog_btn);
mOkBtn.setText(btnContent);
mOkBtn.setOnClickListener(onPoClickListener);
View btnDivider = window.findViewById(R.id.dialog_btn_divider);
btnDivider.setVisibility(View.VISIBLE);
Button mNeBtn = (Button) window.findViewById(R.id.dialog_negative_btn);
mNeBtn.setText(neContent);
mNeBtn.setVisibility(View.VISIBLE);
mNeBtn.setOnClickListener(onNeClickListener);
}
}
/**
* 让Dialog从底部出现/宽度全屏
*
* @param dialog
* @param rootView
*/
public static void setBottom(Dialog dialog, View rootView) {
Window window = dialog.getWindow();
// 在5.0以上手机必须加上这句代码
window.setBackgroundDrawableResource(android.R.color.transparent);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
window.setGravity(Gravity.BOTTOM); //此处可以设置dialog显示的位置
window.setWindowAnimations(R.style.BottomDialogStyle); //添加动画
window.setContentView(rootView);//布局
}
public void show(ShowImageDialogResult result) {
if (mDialog != null && mDialog.isShowing()) {
return;
}
if (!checkPermission(result)) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
mDialog = builder.create();
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.show();
mImageScannerDialogLayout = new ImageScannerDialogLayout(mContext);
mImageScannerDialogLayout.setBackgroundColor(Color.WHITE);
mImageScannerDialogLayout.mDismissDialogListener = new IWidgetClickListener() {
@Override
public void onClick(@Nullable Object o) {
dismiss();
}
};
mImageScannerDialogLayout.mCropImageResultListener = new IWidgetClickListener<String>() {
@Override
public void onClick(@Nullable String imgPath) {
if (mCropImageResultListener != null) {
mCropImageResultListener.onClick(imgPath);
}
dismiss();
}
};
//设置dialog全屏幕
Window window = mDialog.getWindow();
window.setBackgroundDrawableResource(android.R.color.transparent);
window.getDecorView().setPadding(0, 0, 0, 0);
window.setGravity(Gravity.CENTER);
window.setContentView(mImageScannerDialogLayout);
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(lp);
if (result != null) {
result.successful();
}
}
/**
* 进行初始化的操作
*/
private void initView() {
//显示Dialog
dialog = new AlertDialog.Builder(context).create();
dialog.setCancelable(true);
dialog.show();
dialog.getWindow()
.clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialog.getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_MASK_STATE);
//设置Material Design风格的背景
Window window = dialog.getWindow();
View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_base, null, false);
window.setContentView(rootView);
//初始化title部分
fl_base_title = ViewUtil.findViewById(rootView, R.id.fl_base_title);
View titleView = initTitle();
if (null == titleView) {
fl_base_title.setVisibility(View.GONE);
fl_base_title.removeAllViews();
} else {
fl_base_title.setVisibility(View.VISIBLE);
fl_base_title.addView(titleView);
}
//初始化正文部分
fl_base_content = ViewUtil.findViewById(rootView, R.id.fl_base_content);
View contentView = initContent();
if (null == contentView)
throw new UnsupportedOperationException("The dialog must show a view in the window!");
else
fl_base_content.addView(contentView);
//初始化底部
fl_base_bottom = ViewUtil.findViewById(rootView, R.id.fl_base_bottom);
View bottomView = initBottom();
if (null == bottomView)
fl_base_bottom.setVisibility(View.GONE);
else
fl_base_bottom.addView(bottomView);
}