Android:在自定义对话框标题中使用默认样式

14

我试图创建一个自定义的多选警告对话框,允许用户在一次点击中选择/取消选择所有项目。为此,我使用了一个带有额外复选框的自定义标题。

一切都运行得很好,除了我不知道如何使我的自定义标题看起来像默认的警告对话框标题(使用相同的样式)。

这是我想要实现的效果(示例使用对话框文档 中的主题。那只是一个示例,我真正想要的是应用程序主题):

enter image description here

我为使用的自定义标题创建了一个自定义视图,但我不知道如何获取默认样式标题栏的属性,因此我获得:

enter image description here

(标题下没有蓝色条,标题颜色错误)

以下是我的自定义标题布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/title"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Dialog title" />

<TextView
    android:id="@+id/all"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="All" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

我觉得我需要定义标题属性和布局的背景...但我已经花了好几个小时在网上搜索如何获取默认标题视图的属性。

有什么想法吗?


颜色很容易。将文本颜色设置为提供的 Holo 蓝色。 - ChiefTwoPencils
谢谢,但我希望我的标题采用应用程序默认的对话框标题主题。如果应用程序主题不是 Holo,则此方法无效。 - Jean-Marc Astesana
2个回答

10

看看这是否是您在寻找的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="22sp"
            android:textColor="#ff33b5e5"
            android:text="Dialog title" />

        <TextView
            android:id="@+id/all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="All" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <View android:id="@+id/titleDivider"
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:background="#ff33b5e5" />

</LinearLayout>

我是如何得到这个的:

在您的硬盘上浏览到sdk目录 > 平台 > android-XX(例如17)> 数据 > res > 布局 > dialog_title_holo.xml。 查看ID为titleDivider的View。 它的背景属性:background="@android:color/holo_blue_light"。 在res/values/colors.xml中查找此颜色的值。

styles_device_defaults.xml中:

<style name="TextAppearance.DeviceDefault.DialogWindowTitle" parent="TextAppearance.Holo.DialogWindowTitle" >

查看styles.xml文件:

<style name="TextAppearance.Holo.DialogWindowTitle">
    <item name="android:textSize">22sp</item>
    <item name="android:textColor">@android:color/holo_blue_light</item>
</style>

textColor与线条颜色相同。文本大小指定为22sp。因为我们设置了textSize="22sp",所以不需要使用style="?android:attr/textAppearanceLarge"

<style name="TextAppearance.Large">
    <item name="android:textSize">22sp</item>
</style>

3
@Jean-MarcAstesana抱歉如果我给了你“看,我找到了你要找的颜色”的印象。我原本以为我是在告诉你哪里可以找到它。例如,如果你查看dialog_title_holo.xml(这是一个布局文件),你将会得到一些关于安卓如何实现默认对话框中所看到的视图的指示。蓝线的定义与上文完全相同,只不过属性android:background="#ff33b5e5"变成了android:background="@android:color/holo_blue_light" - Vikram
@Jean-MarcAstesana 我能做的最好的就是在标题字段中使用style="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"。没错,(在API14之后的设备上,此样式才存在)。对吧?是的。假设您的minSdkVersion是v13,则可以在res/values-v13/styles.xml中基于@android:style/TextAppearance.DeviceDefault.DialogWindowTitle指定自己的样式。然后,在res/layout-v13/some_layout.xml中使用您自己创建的样式。这需要相当多的工作才能做到正确。 - Vikram
@user2558882:这对我帮助很大。 这正是我想要的那种东西,但这是从xml的角度来看的? 你知道我如何在运行时引用这些属性吗? 我知道有context.obtainStyledAttributes(attrs),但我认为这在当前情况下对我没有帮助。 我基本上需要从当前对话框主题中的dialog_title中提取android:paddingStart和android:paddingEnd值。(在创建期间)。 我知道我不能在运行时读取XML,所以我有点迷失了。 - WORMSS
我现在处于“对话框构建器”阶段。对话框还未被创建。 - WORMSS
@WORMSS 在这种情况下,我能想到的唯一解决方法是:使用 AlertDialogBuilder#create() 创建对话框。使用我之前评论中的方法获取填充值。如果需要,使用这些值与您的 AlertDialogBuilder 一起使用。然后在调用 AlertDialog#show() 之前再次创建对话框。 - Vikram
显示剩余5条评论

1

如果您只想使用设备默认设置,而不需要自定义或扩展任何内容,您可以简单地添加:

android:textAppearance="@android:style/TextAppearance.DeviceDefault.DialogWindowTitle"

在您的XML布局中添加标题视图。这将根据设备主题显示不同的样式。

如果您始终想显示蓝色(Holo主题)标题,而不考虑设备默认设置,则可以添加:

android:textAppearance="@android:style/TextAppearance.Holo.DialogWindowTitle"

instead.


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