繁体   English   中英

自定义AlertDialog标题与默认字体

[英]Custom AlertDialog Title with default font

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

问题:

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

所以基本上我希望第二个屏幕截图的蓝色文字看起来与第一个屏幕截图中的黑色文本完全相同。 我宁愿动态地这样做,而不是仅仅找出AlertDialog使用的确切值并对其进行硬编码。

我的尝试:

我查看了AlertDialog的源代码,发现“AlertDialog使用的实际样式是私有实现”。 但是我也发现他们使用R.attr.alertDialogTheme作为属性, 我可以得到主题的resourceId

我尝试使用该resourceId创建一个TextView,但这不起作用(TextView apeared就像它没有样式)。

默认AlertDialog标题样式:

默认AlertDialog标题样式

我目前的头衔:

我目前的头衔

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

xml的要求是: content.xmltitle.xml (以及colors.xmlstyles.xml

您已经定义的文字颜色 android:textColor="#33B5E5"在你的TextViewtitle.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>

在此输入图像描述

要动态更新视图:

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();

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM