【问题标题】:Custom themes and colors in androidandroid中的自定义主题和颜色
【发布时间】:2016-08-11 14:24:23
【问题描述】:

我可以为两个不同的主题定义两个完全不同的colors.xml 吗?例如,在我的例子中,我们在调色板中使用了更多不同的颜色,这些颜色由标准(android:colorPrimary、android:colorAccent、android:colorBackground 等)在主题中定义。希望,我的问题很清楚。如果不是,请在 cmets 中问我。

【问题讨论】:

  • 你可以为不同的活动创建不同的主题
  • 如果你想在你的应用程序中支持多个主题,你必须针对每个主题声明合适的属性,然后为每个属性定义样式。然后你可以调用活动的方法 setTheme 来设置实际的主题。跨度>
  • 无法创建动态 color.xml。但是您可以创建不同名称的不同资源文件。您可以将这些颜色资源文件用作主题的输入颜色。

标签: android colors styles themes


【解决方案1】:

好的,经过一番搜索,我找到了这样的解决方案。设计师不想使用 colorPrimary 等,所以我在 attrs 中添加我的自定义属性,如下所示:

<attr name="colorExperiment" format="color"/>

在此之后,我在主题的样式中添加这个属性,如下所示:

 <style name="DarkTheme" parent="AppTheme.NoActionBar">
    <item name="android:windowAnimationStyle">@null</item>
    <item name="android:windowBackground">@color/onyx</item>
    <item name="colorExperiment">@color/onyx</item>
</style>

并在布局中使用例如:

<TextView
   android:id="@+id/auth_caption"
   android:includeFontPadding="false"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:gravity="center"
   android:textSize="34dp"
   android:text="@string/auth_caption"
   android:textColor="?colorExperiment"
 />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-04
    • 2017-11-29
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多