android - 如何设置 AlertDialog 自定义标题上边距并删除不需要的 Padding?

标签 android android-alertdialog android-dialog

我遇到了 AlertDialog.Builder 标题的 marginpadding 问题,我正在做的是我想要标题在 center 中,所以我正在使用 setCustomTitle 我能够这样做,但坚持使用边距和填充。我不想显示不需要的填充,我还想为标题设置一些顶部边距,我正在使用 LinearLayout.LayoutParams 但它没有效果。请建议如何处理它。谢谢

代码:

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();

结果:enter image description here

最佳答案

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();

替换这段代码就可以了

关于android - 如何设置 AlertDialog 自定义标题上边距并删除不需要的 Padding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39767895/

相关文章:

android - 使用apktool反编译apk文件

android - 问题添加 Android 对话框

java - 保留警报对话框选项

Android开启Dialog动画监听器

android - opengles2 glGenVertexArrays ... glGenVertexArraysOES

java - 获取 JSON 格式的 POST 结果

android - 为警报对话框设置 Android Theme.Light

android - 在 DialogFragment 中动态添加微调器

android - onPostExecute 中的 AlertDialog 未显示

android - 是否有用于测试的 Android 模拟器工具包?