对话框标题文本大小 Android

Dialog box title text size in Android

我正在开发一个 Android 应用程序,我想在其中设置对话框的文本大小。我已经为 textview 使用了以下代码 XML 但它只是设置了我的字段的大小。我也想设置标题的文字大小和颜色。

final CharSequence[] items = { "Fake Request", "Abusive Language","Indecent Approach","Unacceptable Attitude","Dangerous Behavior",};

           ArrayAdapter<CharSequence> itemsAdapter = new
           ArrayAdapter<CharSequence> (this,
                    R.layout.menu_items, items);
           AlertDialog.Builder   builder = new AlertDialog.Builder(this);
           builder.setTitle("My Title");
           builder.setIcon(R.drawable.icon);
           builder.setAdapter(itemsAdapter, new DialogInterface.OnClickListener()
           {
               public void onClick(DialogInterface dialog, int item) {
                          switch(item) {
                                  case 0:
                              //Mark as Beautiful
                                  break;
                               case 1:
                              //Mark as Beautiful
                                  break;
                            case 2: 
                                //Mark as Not a Portrait
                                  break;
                            case 3:
                              //Mark as Offensive
                                  break;
                           case 4:
                              //Mark as Spam
                                  break;
                           case 5:
                              //cancel
                           break;
                           }
               }
           }

               );
           builder.show();

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@android:id/text1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="10dip"
       android:layout_margin="5dip"
       android:gravity="center_vertical"
       android:textSize="5dip"
       android:textColor="@color/red_theme"
       android:typeface="normal"
       android:lineSpacingExtra="0dip"/>

要实现这一点:

  1. 使用列表视图创建 xml
  2. 使用 inflater 为该视图充气
  3. 在 AndroidBuilder 中设置自定义视图
  4. 使用自定义适配器扩充列表视图中的行。

按照以上几点,您将实现自定义。

您可以使用以下示例:

http://www.mkyong.com/android/android-custom-dialog-example/ http://www.edumobile.org/android/android-development/custom-listview-in-a-dialog-in-android/

希望对你有所帮助

//Use this code
TextView myMsg = new TextView(this);
  myMsg.setText("Succesfully send!");
  myMsg.setGravity(Gravity.CENTER_HORIZONTAL);
  myMsg.setTextSize(20); 
  myMsg.setTextColor(Color.WHITE);
  //set custom title
        builder.setCustomTitle(myMsg);

在 Style.xml 中为对话框创建自定义主题并将其应用到您的对话框。

示例主题:

 <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:bottomBright">@color/white</item>
        <item name="android:bottomDark">@color/white</item>
        <item name="android:bottomMedium">@color/white</item>
        <item name="android:textSize">10</item>
        <item name="android:centerBright">@color/white</item>
        <item name="android:centerDark">@color/white</item>
        <item name="android:centerMedium">@color/white</item>
        <item name="android:fullBright">@color/orange</item>
        <item name="android:fullDark">@color/orange</item>
        <item name="android:topBright">@color/blue</item>
        <item name="android:topDark">@color/blue</item>
    </style>

在你代码中:

AlertDialog.Builder   builder = new AlertDialog.Builder(this, R.style.CustomDialogTheme);

使用自定义标题使用 builder.setCustomTitle(textView) 设置标题的大小和颜色。

AlertDialog.Builder builder = new AlertDialog.Builder(context);
    TextView title = new TextView(context);
    title.setText("Title");
    title.setBackgroundResource(R.drawable.gradient);
    title.setPadding(10, 10, 10, 10);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(getResources().getColor(R.color.green));
    title.setTextSize(22);
    builder.setCustomTitle(title);
    builder.setMessage("Message");
    builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

    }
    });
    builder.setNegativeButton(R.string.no, null);
    AlertDialog dialog = builder.show();
    messageView = (TextView)dialog.findViewById(android.R.id.message);
    messageView.setGravity(Gravity.CENTER);
    dialog.show();

这是最简单的方法

先自定义一个textView,

TextView dialogTitle = new TextView(getApplicationContext());
dialogTitle.setText("Your Title Text");
dialogTitle.setTextColor(getResources().getColor(R.color.your_color));
dialogTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);

然后写

builder.setCustomTitle(dialogTitle);

而不是

builder.setTitle("My title");

完成!

如果您想全部使用样式:

<style name="MyDialog" parent="Base.Theme.AppCompat.Light.Dialog">
    <item name="android:windowTitleStyle">@style/DialogTitle</item>
</style>

<style name="DialogTitle" parent="TextAppearance.AppCompat.Large">
    <item name="android:textSize">18sp</item>
</style>

并创建你的对话框

new AlertDialog.Builder(this, R.style.MyDialog);

有点晚了...但是如果有人有同样的问题:

androidx.appcompat.app.AlertDialog

AlertDialog alertdialog;
...
alertdialog.show();
final View v = alertdialog.findViewById(R.id.title_template);
    if (v != null) {
        v.setBackgroundColor(getResources().getColor(colorPrimaryDark));
        TextView title = v.findViewById(R.id.alertTitle);
        title.setTextSize(30);
    }