Android AppCompat AlertDialog 样式与主题
Android AppCompat AlertDialog styling with theme
我希望能够设置主题以设置 AppCompat AlertDialog 中的 message 文本大小。主题需要 parent="@style/Theme.AppCompat.Dialog"
。我花了数小时搜索和尝试所有建议,但其中 none 似乎适用于该基本主题。
如果父级更改为 Holo 主题,那么我可以使用 textAppearanceMedium
更改消息文本大小,但对话框的其余部分看起来真的很难看 :S
目前我的主题是(所有这些目前都已连接并正在运行):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyDialogTheme" parent="@style/Theme.AppCompat.Dialog">
<!-- Used for the buttons -->
<item name="colorAccent">@color/colorPrimary</item>
<!-- Button text size -->
<item name="android:textSize">@dimen/ui_text_size</item>
<!-- Content text color -->
<item name="android:textColorPrimary">@color/ui_text_color</item>
<!-- Title style -->
<item name="android:windowTitleStyle">@style/MyDialogTitleStyle</item>
<!-- Button style (except size) -->
<item name="android:textAppearanceButton">@style/MyDialogButtonTextAppearance</item>
<!-- Dialog background -->
<item name="android:windowBackground">@color/ui_background</item>
</style>
<style name="MyDialogTitleStyle" parent="@style/RtlOverlay.DialogWindowTitle.AppCompat">
<item name="android:textAppearance">@style/MyDialogTitleTextAppearance</item>
<item name="android:textSize">@dimen/ui_large_text_size</item>
</style>
<style name="MyDialogTitleTextAppearance">
<item name="android:textSize">@dimen/ui_large_text_size</item>
<item name="android:textAllCaps">true</item>
<item name="android:textColor">@color/ui_title_color</item>
</style>
<style name="MyDialogButtonTextAppearance">
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textAllCaps">true</item>
</style>
</resources>
似乎无法通过主题属性来实现。我们看一下appcompat-v7 library的源码。在反映 AlertDialog
消息的 TextView
之后:
<android.support.v7.widget.AlertDialogLayout ... >
<!-- ... -->
<TextView
android:id="@android:id/message"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"/>
<!-- ... -->
</android.support.v7.widget.AlertDialogLayout>
如您所见,TextView
使用 TextAppearance.AppCompat.Subhead
作为样式。按照其定义:
<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead"/>
<style name="Base.TextAppearance.AppCompat.Subhead">
<item name="android:textSize">@dimen/abc_text_size_subhead_material</item>
<item name="android:textColor">?android:textColorPrimary</item>
</style>
textSize
是静态的,不会通过 textColor
(使用 textColorPrimary
)之类的属性解析。因此我们没有设置文本大小的选项。唯一的方法是通过将 TextAppearance.AppCompat.Subhead
样式添加到您自己的 styles.xml
文件并将 textSize
设置为您需要的任何值来覆盖 TextAppearance.AppCompat.Subhead
样式。但要注意可能会有副作用,因为这种风格也可以用在其他地方。
tl;博士
选项:
- 在 styles.xml 文件中定义
TextAppearance.AppCompat.Subhead
并覆盖 textSize
属性。
- 以编程方式执行(通过 id 和 #setTextSize 查找
TextView
)
- 在对话框中使用您自己的布局 - appcompat-v7 的源代码可能是一个很好的起点。
我希望能够设置主题以设置 AppCompat AlertDialog 中的 message 文本大小。主题需要 parent="@style/Theme.AppCompat.Dialog"
。我花了数小时搜索和尝试所有建议,但其中 none 似乎适用于该基本主题。
如果父级更改为 Holo 主题,那么我可以使用 textAppearanceMedium
更改消息文本大小,但对话框的其余部分看起来真的很难看 :S
目前我的主题是(所有这些目前都已连接并正在运行):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyDialogTheme" parent="@style/Theme.AppCompat.Dialog">
<!-- Used for the buttons -->
<item name="colorAccent">@color/colorPrimary</item>
<!-- Button text size -->
<item name="android:textSize">@dimen/ui_text_size</item>
<!-- Content text color -->
<item name="android:textColorPrimary">@color/ui_text_color</item>
<!-- Title style -->
<item name="android:windowTitleStyle">@style/MyDialogTitleStyle</item>
<!-- Button style (except size) -->
<item name="android:textAppearanceButton">@style/MyDialogButtonTextAppearance</item>
<!-- Dialog background -->
<item name="android:windowBackground">@color/ui_background</item>
</style>
<style name="MyDialogTitleStyle" parent="@style/RtlOverlay.DialogWindowTitle.AppCompat">
<item name="android:textAppearance">@style/MyDialogTitleTextAppearance</item>
<item name="android:textSize">@dimen/ui_large_text_size</item>
</style>
<style name="MyDialogTitleTextAppearance">
<item name="android:textSize">@dimen/ui_large_text_size</item>
<item name="android:textAllCaps">true</item>
<item name="android:textColor">@color/ui_title_color</item>
</style>
<style name="MyDialogButtonTextAppearance">
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textAllCaps">true</item>
</style>
</resources>
似乎无法通过主题属性来实现。我们看一下appcompat-v7 library的源码。在反映 AlertDialog
消息的 TextView
之后:
<android.support.v7.widget.AlertDialogLayout ... >
<!-- ... -->
<TextView
android:id="@android:id/message"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="?attr/dialogPreferredPadding"
android:paddingRight="?attr/dialogPreferredPadding"/>
<!-- ... -->
</android.support.v7.widget.AlertDialogLayout>
如您所见,TextView
使用 TextAppearance.AppCompat.Subhead
作为样式。按照其定义:
<style name="TextAppearance.AppCompat.Subhead" parent="Base.TextAppearance.AppCompat.Subhead"/>
<style name="Base.TextAppearance.AppCompat.Subhead">
<item name="android:textSize">@dimen/abc_text_size_subhead_material</item>
<item name="android:textColor">?android:textColorPrimary</item>
</style>
textSize
是静态的,不会通过 textColor
(使用 textColorPrimary
)之类的属性解析。因此我们没有设置文本大小的选项。唯一的方法是通过将 TextAppearance.AppCompat.Subhead
样式添加到您自己的 styles.xml
文件并将 textSize
设置为您需要的任何值来覆盖 TextAppearance.AppCompat.Subhead
样式。但要注意可能会有副作用,因为这种风格也可以用在其他地方。
tl;博士
选项:
- 在 styles.xml 文件中定义
TextAppearance.AppCompat.Subhead
并覆盖textSize
属性。 - 以编程方式执行(通过 id 和 #setTextSize 查找
TextView
) - 在对话框中使用您自己的布局 - appcompat-v7 的源代码可能是一个很好的起点。