【问题标题】:How to allow users to choose themes in android list view?如何允许用户在 android 列表视图中选择主题?
【发布时间】:2010-06-10 17:25:08
【问题描述】:

我有一个带有两个标签的列表视图,标题和副标题。我希望有深色和浅色背景作为用户选项。标题有 textAppearanceMedium,副标题有 textAppearanceSmall。我想要样式 MyTheme.Dark 有白色文本和 MyTheme.Light 有黑色文本。有没有办法为同一个 TextView 小部件定义多个 textAppearance 属性?

<style name="MyTheme.Dark">
    <item name="android:windowBackground">@color/black</item>
    <item name="android:colorBackground">@color/black</item>
    <item name="android:background">@color/black</item>
    <item name="android:divider">@color/white</item>
        --cannot put textAppearance here since it is different for title and subtitle
    </style>

【问题讨论】:

    标签: android themes


    【解决方案1】:

    供以后参考...

    在 values/themes.xml 中:

    <style name="MyTheme.Dark">
        <item name="textColorTitle">@color/white</item>
    </style>
    <style name="MyTheme.Light">
        <item name="textColorTitle">@color/black</item>
    </style>
    

    在值/attrs.xml 中:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr
            name="textColorTitle"
            format="reference|color" />
    </resources>
    

    在 layout/my_list_row.xml 中,使用 textColorTitle 值作为文本颜色,如下所示:

    android:textColor="?textColorTitle"
    

    如果它不起作用,请告诉我。它对我有用。

    【讨论】:

      【解决方案2】:

      我自己是 Android 的初学者,我不确定我是不是在这里自作多情,但你为什么不使用 android:textColor 属性?

      <style name="MyTheme.Dark">
          <item name="android:windowBackground">@color/black</item>
          <item name="android:colorBackground">@color/black</item>
          <item name="android:background">@color/black</item>
          <item name="android:divider">@color/white</item>
          <item name="android:textColor">@color/white</item>
      </style>
      

      【讨论】:

      • 主题将应用于活动。我将有其他不同的标题和副标题的文本属性。看来我需要重新表述我的问题。
      • 系统定义的 textAppearance 样式已指定颜色、大小。因此,主题中设置的颜色不适用于 TextView 小部件。尝试定义我自己的 textAppearance 样式仍然没有运气。即使我没有指定它,ListView 也会得到一些 listAppearance。
      【解决方案3】:

      我认为最好的方法是为不同的颜色和主题设置不同的布局 xml。根据用户选择加载布局 xml。我不喜欢这个,但我没有找到任何替代品。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-27
        • 1970-01-01
        • 2016-10-10
        • 2020-09-07
        • 2015-02-08
        • 2013-08-06
        • 2014-10-20
        相关资源
        最近更新 更多