前言
记录一些自己项目上自定义的Dialog
泛用性基础Dialog
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class BaseDialog extends Dialog {
private float widthRatio = 1f;
private float heightRatio = 1f;
private boolean mIsShowNavigation = false;
public BaseDialog(@NonNull Context context) {
super(context);
}
public BaseDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
/**
* 设置对话框宽度与屏幕宽度占比 0.0~1.0
*
* @param widthRatio
*/
public void setWidthHeightRatio(float widthRatio, float heightRation) {
this.widthRatio = widthRatio;
this.heightRatio = heightRation;
}
public void setShowNavigation(boolean isShowNavigation) {
mIsShowNavigation = isShowNavigation;
}
@Override
protected void onStart() {
super.onStart();
//隐藏导航栏
if (!mIsShowNavigation) {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
getWindow().setAttributes(params);
}
//去除对话框内边距与背景透明
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
if (widthRatio == 1) {
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
} else {
float width = getContext().getResources().getDisplayMetrics().widthPixels * widthRatio;
layoutParams.width = (int) width;
}
if (heightRatio == 1) {
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
} else {
float height = getContext().getResources().getDisplayMetrics().heightPixels * heightRatio;
layoutParams.height = (int) height;
}
getWindow().setAttributes(layoutParams);
getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
getWindow().getDecorView().setPadding(0, 0, 0, 0);
}
}
实现对话框全透明
在styles添加
<style name="dialogTransparent" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!--边框--> <item name="android:windowIsFloating">true</item> <!--是否浮现在activity之上--> <item name="android:windowIsTranslucent">true</item> <!--半透明--> <item name="android:windowNoTitle">true</item> <!--无标题--> <item name="android:background">@android:color/transparent</item> <!--背景透明--> <item name="android:windowBackground">@android:color/transparent</item> <!--背景透明--> <item name="android:backgroundDimEnabled">true</item> <!--模糊--> <item name="android:backgroundDimAmount">0.5</item> <!--背景透明度0到1 50%就是0.5--> </style>
将风格添加
public class NumberInputDialog extends BottomSheetDialog implements View.OnClickListener { public NumberInputDialog(@NonNull Context context) { super(context,R.style.dialogTransparent); initView(); } //略...
设置背景透明
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT)); }
设置全屏
请注意,在开发的时候经常会发现,我们设置的全屏的宽度或者全屏的高度后经常外面还要一些边缘空白空间,需要设置setPadding来取消空白内边距,代码如下:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().getDecorView().setPadding(0,0,0,0);//取消内边距空白 getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//设置全屏 }
或者
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().getDecorView().setPadding(0,0,0,0);//取消内边距空白 WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; getWindow().setAttributes(layoutParams); }
在取消内边距空白后,还有一个好处,那就是设置 layoutParams.width 与layoutParams.height 会更加准确
设置对话框显示位置
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置显示位置 Gravity.BOTTOM Gravity.TOP Gravity.LEFT Gravity.RIGHT Gravity.CENTER getWindow().setGravity(Gravity.BOTTOM); }
弹出输入法并且焦点选中输入框
EditText editTextTextPersonName = findViewById(R.id.editTextTextPersonName); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);//设置输入盘可见 editTextTextPersonName.requestFocus();
设置背景阴影透明度
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.dimAmount = 0f;//调整透明度 getWindow().setAttributes(layoutParams); }
BottomSheetDialog禁用下滑退出
private fun showDurationPickerDialog(){ val dialog = context?.let { BottomSheetDialog(it) } val binding = DialogDurationPickerBinding.inflate(layoutInflater) dialog?.setContentView(binding.root) binding.root.post { val view = dialog?.findViewById<View>(R.id.design_bottom_sheet) val behavior = view?.let { BottomSheetBehavior.from(it) } behavior?.isHideable = false } //略...
BottomSheetDialog背景透明(显示圆角)
BottomSheetDialog对话框其实是自带一个白色背景的,我们在自己的布局设置圆角shape后会出现不显示的情况.
val dialog = BottomSheetDialog(this) dialog.setContentView(R.layout.main_dialog_bottom_wheel) dialog.show() //设置背景透明,需要在show方法后面 dialog.findViewById<View>(R.id.design_bottom_sheet)?.setBackgroundResource(android.R.color.transparent)
横屏显示不全的问题解决
@Override protected void onStart() { super.onStart(); if (mIsHorizontal){ getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED); } }
隐藏状态栏与导航栏
@Override protected void onStart() { super.onStart(); //略... //隐藏导航栏 WindowManager.LayoutParams params = getWindow().getAttributes(); params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; getWindow().setAttributes(params); }
end
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/14154232.html
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。