今⽇头条适配⽅案造成Dialog显⽰异常的解决⽅法
今⽇头条适配⽅案造成Dialog显⽰异常的解决⽅法
产⽣问题的原因
由于该适配⽅案是更改的全局的density, 对整个项⽬⽣效,所以在使⽤第三⽅View或者系统的的view时,如果和项⽬中的设计尺⼨差别很⼤,就会出现很严重的UI问题。
解决办法
⽅法⼀: 尽量所有的视图都使⽤⾃定义的视图。
⽅法⼆: 更改原⽣Dialog的⼤⼩。
代码⽰例⼀: 根据屏幕宽度, 更改原⽣Dialog的⽐例
/**
* 显⽰取消/确认的dialog
*/
public void showConfirmDialog(int title, int positiveButtonText, int negativeButtonText, ConfirmDialogCallback callback) {
if (mActivity == null) return;
mAlertDialog = new AlertDialog.Builder(mActivity).setTitle(title).setCancelable(false)
.setPositiveButton(positiveButtonText, (dialogInterface, i) -> {
if (callback != null)
}).setNegativeButton(negativeButtonText, (dialogInterface, i) -> {
if (callback != null)
callback.cancel();
}).show();
if (Window() != null) {
alertdialog使用方法WindowManager.LayoutParams lp = Window().getAttributes();
lp.width = WindowManager().getDefaultDisplay().getWidth() / 10 * 8; // 宽度,可根据屏幕宽度进⾏计算
}
}
代码⽰例⼆: 重写 AlertDialog.Builder,把项⽬的设计尺⼨给原⽣的Dialog
public class BaseAlertDialogBuilder extends AlertDialog.Builder {
/**
* 设计图尺⼨
*/
private static final float ALERT_BASE_WIDTH = 420f;
public BaseAlertDialogBuilder(@NonNull Context context) {
super(adjustAutoSize(context));
}
public BaseAlertDialogBuilder(@NonNull Context context, int themeResId) {
super(adjustAutoSize(context), themeResId);
}
private static Context adjustAutoSize(Context context) {
return new ContextWrapper(context) {
private Resources mResources;
{
Resources oldResources = Resources();
mResources = new Assets(), DisplayMetrics(), Configuration()); }
@Override
public Resources getResources() {
// 根据横竖屏状态需要更改。
AutoSizeCompat.autoConvertDensityBaseOnWidth(mResources, ALERT_BASE_WIDTH);
// AutoSizeCompat.autoConvertDensityBaseOnHeight(mResources, ALERT_BASE_WIDTH);
return mResources;
}
};
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论