自定义AlertDialog标题,默认字体为

时间:2017-05-09 18:46:14

标签: java android xml android-layout android-alertdialog

我的应用程序中的所有对话框都是使用AlertDialog构建的,但我的蓝牙选择器对话框除外。我的目标是为此创建一个AlertDialog。

问题:

如果我想在右上方添加旋转进度条,我需要使用AlertDialog的Builder的.setCustomTitle(view)方法。这有效,但我也想为这个标题使用默认的AlertDialog标题样式,但我不确定如何做到这一点。

所以基本上我希望第二张截图的蓝色文字看起来与第一张截图中的黑色文字完全相同。我更喜欢动态地执行此操作,而不是仅仅找出AlertDialog使用的确切值并对其进行硬编码。

我的尝试:

我查看了AlertDialog的源代码,发现" AlertDialog使用的实际样式是私有实现"。但是我也发现他们使用R.attr.alertDialogTheme作为属性和I can get the resourceId of the theme

我尝试使用该resourceId创建一个TextView但是没有工作(TextView就像它没有样式一样)。

默认AlertDialog标题样式:

Default AlertDialog title style

我目前的头衔:

My current title

您可以使用以下代码重新创建上面显示的对话框:https://gist.github.com/anonymous/34edc1afbdb7e9d10d9adbda63d4cd8c

xml所需的是:content.xmltitle.xml(以及colors.xmlstyles.xml

1 个答案:

答案 0 :(得分:3)

您已经在 android:textColor="#33B5E5" 中定义了 textView textColor title.xml只需将其更改为您想要的颜色。

以下是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="24dp"
    android:paddingRight="24dp"
    android:paddingTop="9dp"
    android:paddingBottom="18dp">
<TextView
    android:id="@+id/customTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Custom title view"
    android:textColor="#000000"
    android:textSize="24sp"
    android:layout_centerVertical="true"/>

<TextView
    android:id="@+id/anotherText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/customTitle"
    android:paddingTop="9dp"
    android:text="Another TextView"
    android:textColor="@android:color/holo_purple" />

    <LinearLayout
        android:id="@+id/lin_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:orientation="vertical"
        android:layout_centerVertical="true">

        <ProgressBar
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/loading_spinner"
            android:visibility="invisible"/>

    </LinearLayout>

</RelativeLayout>

enter image description here

动态更新视图:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        final View yourTitleView = inflater.inflate(R.layout.title, null);
        TextView yourTitle = (TextView) yourTitleView.findViewById(R.id.customTitle);
        yourTitle.setText("Your new title text");//Dynamical text
        yourTitle.setTextColor(Color.parseColor("#ffa000")); //Dynamical color

        //Your AlertDialog
        new AlertDialog.Builder(this)
                .setCustomTitle(yourTitleView) //The title's TextView's style should be the
                // same as the the one of the dialog normal AlertDialog's Title Style (see example above)
                .setView(inflater.inflate(R.layout.content, null))
                .setPositiveButton("Start", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                })
                .show();

不要忘记为自己的观点定义好的ID。

对于另一个误解

要使用自定义对话框格式化文本,只需使用Html函数

Html.fromHtml("<font color='#000000'> Your black text </font>")

示例:

 new AlertDialog.Builder(YourActivity.this)
         .setTitle(Html.fromHtml("<font color='#000000'> Your black title </font>"))
         .setView(yourView)
         .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                      //Stuff to do
                   }
              })
         .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                      //Stuff to do
                      dialog.dismiss();
                   }
               })
         .show();

希望它有所帮助。

相关问题