本文介绍了如何设置AlertDialog自定义标题的上边距并删除不需要的填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了 AlertDialog.Builder 标题的 margin padding 问题,我在做的是我想要标题在 center 中,所以我可以使用 setCustomTitle 来执行此操作,但会留有边距和填充.我不想显示不必要的填充,我也想为标题设置一些上边距,我正在使用 LinearLayout.LayoutParams ,但它没有任何作用.请提出处理建议.谢谢

I am stuck with a margin and padding issue of a title of AlertDialog.Builder, what i am doing is i want the title in center so i am using setCustomTitle i am able to do so but stuck with margin and padding of it. I don't want unwanted padding which is showing and also i want to set some top margin to title, i am using LinearLayout.LayoutParams but it has no effect. please suggest what to do to handle it.thanks

代码:

dialog = new AlertDialog.Builder(context, R.style.DialogTheme);
TextView title = new TextView(context);
title.setTextColor(ContextCompat.getColor(context, R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 20, 0, 0);
title.setLayoutParams(lp);
title.setText("Dialog");
title.setGravity(Gravity.CENTER);
dialog.setCustomTitle(title);
dialog.setMessage("Dialog box with custom title view ");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});
dialog.show();

结果:

推荐答案

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
TextView title = new TextView(this);
title.setTextColor(ContextCompat.getColor(this, android.R.color.black));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
title.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 20, 0, 0);
title.setPadding(0,30,0,0);
title.setLayoutParams(lp);
title.setText("Dialog");
title.setGravity(Gravity.CENTER);
dialog.setCustomTitle(title);
dialog.setMessage("Dialog box with custom title view ");
dialog.setCancelable(false);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});
dialog.show();

替换此代码即可使用

这篇关于如何设置AlertDialog自定义标题的上边距并删除不需要的填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 15:55