【问题标题】:How to set AlertDialog custom title top margin and remove unwanted Padding?如何设置 AlertDialog 自定义标题上边距并删除不需要的填充?
【发布时间】:2016-09-29 10:33:39
【问题描述】:

我被marginpadding 标题为AlertDialog.Builder 的问题困住了,我正在做的是我想要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();

结果:

【问题讨论】:

  • 看起来不错!我没有看到填充有任何问题。如果你想在dialog setContentView() 中使用重力概念,你可以居中文本!
  • @utkarshdubey 我不想在对话框标题和默认情况下出现的对话框消息之间有太多填充

标签: android android-alertdialog android-dialog


【解决方案1】:
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();

替换这段代码就可以了

【讨论】:

  • 感谢setPadding 有效,但如何删除对话框标题和消息之间的填充任何想法?
  • @KapilRajput 尝试设置自定义对话框而不是警报对话框它将提供更多自定义
【解决方案2】:

您可以使用 DialogFragment 避免将所有这些方法设置到您的 AlertDialog。只需在onCreateView 方法中使用getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 并以创建通用片段的方式为对话框制作自己的自定义视图。这是进行正确对话的更好方法。

【讨论】:

    【解决方案3】:

    我建议您使用 DialogFragment 将自定义布局显示为警报对话框非常简单,我们可以根据需要自定义我们的对话框..有关它的更多详细信息:https://developer.android.com/reference/android/app/DialogFragment.html

    下面是根据您的要求的布局,您可以使用下面的 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>
    

    【讨论】:

      猜你喜欢
      • 2014-03-12
      • 1970-01-01
      • 2022-06-17
      • 2021-07-17
      • 2019-11-17
      • 2022-12-31
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多