Android 如何设置AlertDialog自定义标题顶部边距并删除不需要的填充?
我被AlertDialog标题的边距和填充问题所困扰。Builder,我所做的是我希望标题位于中间,因此我使用setCustomTitle我可以这样做,但被它的边距和填充所困扰。我不希望出现不必要的填充,而且我希望为标题设置一些上边距,我使用LinearLayout.LayoutParams,但它没有效果。请建议如何处理。谢谢 代码:Android 如何设置AlertDialog自定义标题顶部边距并删除不需要的填充?,android,android-alertdialog,android-dialog,Android,Android Alertdialog,Android Dialog,我被AlertDialog标题的边距和填充问题所困扰。Builder,我所做的是我希望标题位于中间,因此我使用setCustomTitle我可以这样做,但被它的边距和填充所困扰。我不希望出现不必要的填充,而且我希望为标题设置一些上边距,我使用LinearLayout.LayoutParams,但它没有效果。请建议如何处理。谢谢 代码: dialog = new AlertDialog.Builder(context, R.style.DialogTheme); TextView title =
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();
结果:
替换此代码,它将正常工作
替换此代码它将起作用您可以通过使用DialogFragment避免将所有这些方法设置为AlertDialog。只需使用getDialog.getWindow.requestFeatureWindow.FEATURE\u NO\u TITLE;在onCreateView方法中,以创建公共片段的方式为对话框创建自己的自定义视图。使用DialogFragment可以避免在AlertDialog中设置所有这些方法。只需使用getDialog.getWindow.requestFeatureWindow.FEATURE\u NO\u TITLE;在onCreateView方法中,以创建公共片段的方式为对话框创建自己的自定义视图。这是制作正确对话框的更好方法。我建议您使用DialogFragment将自定义布局显示为警报对话框。这非常简单,我们可以根据需要自定义对话框。有关详细信息: 下面是根据您的要求的布局,您可以使用下面的xml布局来显示您的自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/margin10dp"
android:layout_weight="1"
android:gravity="center"
android:text="My Dialog"
android:textColor="@color/white"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:padding="10dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="My Dialog Box Description !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:background="@android:color/transparent"
android:text="OK"
android:textSize="22dp" />
</RelativeLayout>
</RelativeLayout>
我建议您使用DialogFragment将自定义布局显示为警报对话框。这非常简单,我们可以根据需要自定义对话框。有关详细信息: 下面是根据您的要求的布局,您可以使用下面的xml布局来显示您的自定义布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/margin10dp"
android:layout_weight="1"
android:gravity="center"
android:text="My Dialog"
android:textColor="@color/white"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/header"
android:padding="10dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="My Dialog Box Description !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:background="@android:color/transparent"
android:text="OK"
android:textSize="22dp" />
</RelativeLayout>
</RelativeLayout>
看起来不错!我看不出填充有任何问题。如果你想在对话框setContentView中使用重力概念,你可以将文本居中@utkarshdubey我不想在对话框标题和对话框消息之间有太多的空白,这是由defaultLooks产生的结果!我看不出填充有任何问题。如果你想在对话框setContentView中使用重力概念,你可以将文本居中@utkarshdubey我不希望对话框标题和对话框消息之间有太多的填充,这是默认设置的结果。感谢设置填充工作,但如何删除对话框标题和消息之间的填充?有什么想法吗?@KapilRajput尝试设置自定义对话框而不是警报对话框。它将提供更多自定义谢谢设置填充工作,但如何删除对话框标题和消息之间的填充有什么想法吗?@KapilRajput尝试设置自定义对话框而不是警报对话框,这将提供更多自定义