我目前正在尝试建立自己的DialogFragment主题。

我的要求之一是在标题的右侧使用一个图标。

第一个想法

只需使用:

this.getDialog().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title);

但是不幸的是,这行代码没有结果。

第二个想法

使用此textview提供我自己的布局-this.setContentView()-:
<TextView
    android:id="@+id/tvTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="From XML"
    android:textAppearance="@android:style/TextAppearance.DialogWindowTitle" />

这行得通,因为我可以阅读文本,但仍使用默认的黑色字体书写。

我期望TextAppearance.DialogWindowTitle给我的文本标题标题(Holo中的蓝色,大字体)

有什么想法或建议吗?

编辑

这几乎达到了目的:
<style name="Dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
</style>

<style name="MyOwnDialogTitle">
    <item name="android:drawableRight">@drawable/MyRightImage</item>
</style>

但是android:drawableRight刚刚打破了标题布局:

第二编辑

这样好一点:
<style name="Dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
</style>

<style name="MyOwnDialogTitle">
    <item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
    <item name="android:drawableRight">@drawable/icon</item>
</style>

最佳答案

也许您可以通过以下方式扩展默认样式:

res/values/dialogstyles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
    </style>
    <style name="MyOwnDialogTitle">
        <item name="android:drawableRight">@drawable/MyRightImage</item>
    </style>
</resources>

res/values-v11/dialogstyles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
     <style name="Dialog" parent="@android:style/Theme.Holo.Dialog">
          <item name="android:windowTitleStyle">@style/MyOwnDialogTitle</item>
     </style>
</resources>

并覆盖您的DialogFragment的onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog);
}

Working Example

10-08 03:09