为AlertDialog设置和更新自定义视图

4
我的应用程序中所有的对话框都是用AlertDialog构建的,除了我的蓝牙选择器对话框。我的目标是为此创建一个AlertDialog。
问题:
如果我想在右上角添加旋转进度条,我需要使用AlertDialog的Builder的.setCustomTitle(view)方法。这个方法可以实现,但我希望对于这个标题也使用默认的AlertDialog标题样式,但我不确定如何做到这一点。
因此,基本上我希望第二个屏幕截图中的蓝色文本与第一个屏幕截图中的黑色文本完全相同。我更喜欢动态完成这项工作,而不是找出AlertDialog使用的确切值并硬编码它们。
我的尝试:
我查看了AlertDialog的源代码,并发现“AlertDialog实际使用的样式是私有实现”。但是我还发现他们使用R.attr.alertDialogTheme属性和{{link1:我可以获取主题的resourceId}}。
我尝试使用该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个回答

3

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

以下是一个示例:

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

希望它有帮助。

我觉得你没理解我的意思,我想在标题栏的右上角添加一个旋转进度条。这应该通过使用 'AlertDialog.Builder(this).setCustomTitle(view)' 来完成,但此时我需要在布局中使用 TextView 来代替默认的 .setTitle()。因此,目标是将 TextView 的文本颜色和大小设置为与 AlertDialog 完全相同的值,最好是动态地(因为确切的颜色/大小可能会更改且不在我的控制范围内)。如果无法获取完全一致的 AlertDialog 样式,则希望尽可能接近。 - Henrykvdb
第一张屏幕截图展示的是我想要文本看起来的样子,即只有普通的".setTitle("我的文本在这里")",不包含HTML代码。 - Henrykvdb
好的,我明白了。我看到你更新了你的问题,请提供你的视图(布局)和样式和颜色(值)的XML文件。以及你用来填充它们的代码。 - Brandon Zamudio
1
我更新了我的回答,这不是我预期的任何事情。 - Brandon Zamudio
1
好的,无论如何我都会更新一些基本代码以实现动态更新视图。 - Brandon Zamudio
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接