【问题标题】:Android AppCompat AlertDialog styling with theme带有主题的 Android AppCompat AlertDialog 样式
【发布时间】:2017-07-31 05:34:55
【问题描述】:

我希望能够设置主题以在 AppCompat AlertDialog 中设置 message 文本大小。主题需要有parent="@style/Theme.AppCompat.Dialog"。我花了几个小时搜索和尝试所有建议,但似乎没有一个适用于该基本主题。

如果父级更改为 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>

【问题讨论】:

  • 您也可以创建自定义对话框。
  • 可以发Material主题和holo主题的截图吗?

标签: android android-appcompat android-theme android-styles


【解决方案1】:

似乎没有办法通过主题属性来做到这一点。我们来看appcompat-v7 library的源码。在TextView后面反映了AlertDialog的消息:

<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)。因此我们没有设置 textSize 的选项。唯一的方法是通过将TextAppearance.AppCompat.Subhead 添加到您自己的styles.xml 文件并将textSize 设置为您需要的任何值来覆盖TextAppearance.AppCompat.Subhead 样式。但请注意可能会有副作用,因为这种风格也可以在其他地方使用。

tl;博士

选项:

  1. 在您的styles.xml 文件中定义TextAppearance.AppCompat.Subhead 并覆盖textSize 属性。
  2. 以编程方式执行(通过 id 和 #setTextSize 查找 TextView)
  3. 在对话框中使用您自己的布局 - appcompat-v7 的源代码可能是一个很好的起点。

【讨论】:

  • 感谢@reVerse,定义我自己的TextAppearance.AppCompat.Subhead 工作很愉快!
猜你喜欢
  • 2015-08-31
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 2016-11-08
相关资源
最近更新 更多