[英]How to set AlertDialog custom title top margin and remove unwanted Padding?
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. 我遇到了AlertDialog.Builder
标题的margin
和padding
问题,我正在做的是我想要标题center
因此我可以使用setCustomTitle
做到这一点,但遇到了边距和填充问题。 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. 我不想显示不必要的填充,我也想为标题设置一些上边距,我正在使用LinearLayout.LayoutParams
但没有任何效果。 please suggest what to do to handle it.thanks 请提出处理建议。谢谢
Code : 代码:
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();
Replace this code it will work 替换此代码将起作用
You can avoid setting up all this methods to your AlertDialog by using DialogFragment. 您可以避免使用DialogFragment为您的AlertDialog设置所有这些方法。 Just use getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
只需使用getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
in onCreateView
method and make your own custom view for the dialog in the way you create a common fragment. 在onCreateView
方法中,并以创建公共片段的方式为对话框创建自己的自定义视图。 It's a better way to make proper dialogs. 这是进行正确对话的更好方法。
I suggest you to use DialogFragment to show custom layout as alert Dialog its very easy and we can customize our dialog as per requirement..for more detail about it : https://developer.android.com/reference/android/app/DialogFragment.html 我建议您使用DialogFragment将自定义布局显示为警报对话框,它非常容易,我们可以根据要求自定义对话框。有关其的更多详细信息: https : //developer.android.com/reference/android/app/DialogFragment .html
Below is the layout as per your requirement, You can use the below xml layout to show your custom layout. 下面是根据您的要求的布局,您可以使用下面的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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.