为不同主题配置不同的颜色

问题描述 投票:0回答:1

我正在尝试配置DayNight主题。

我的values / style.xml和(values-night / style.xml)是:

<resources>
  <!-- Base application theme. -->
  <style name="AppTheme" parent="Theme.AppCompat.DayNight">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
  </style>
  <style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>
  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
</resources>

colors.xml是:

<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="lSun">#ff9800</color>
    <color name="lMoon">#3f51b5</color>
    <color name="lMoonDark">#4f9fbd</color>
</resources>

现在,喜欢,

 <TextView
              android:id="@+id/mr_icon"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/longi"
              android:layout_marginStart="16dp"
              android:layout_marginTop="5dp"
              android:layout_toEndOf="@+id/sunset"
              android:fontFamily="@font/weathericons"
              android:paddingTop="-2dp"
              android:text="@string/moonrise"
              android:textAlignment="center"
              android:textAppearance="@android:style/TextAppearance.Material.Menu"
              android:textColor="@color/lMoon"
              android:textSize="14sp" />

这里,我想要@color/lMoon代表浅色主题,@color/lMoonDark代表深色主题。

我该如何实现?

android android-theme
1个回答
0
投票

制作两种样式,并将所需样式提供给TextView。

<style name="MyTextView" parent="@android:style/TextAppearance.Small">
   <item name="android:layout_width">wrap_content</item>
   <item name="android:layout_height">wrap_content</item>
   <item name="android:textColor">@android:color/white</item>
   <item name="android:textSize">12sp</item>
</style>

<TextView
          style="@style/MyTextView"
          android:id="@+id/mr_icon"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/longi"
          android:layout_marginStart="16dp"
          android:layout_marginTop="5dp"
          android:layout_toEndOf="@+id/sunset"
          android:fontFamily="@font/weathericons"
          android:paddingTop="-2dp"
          android:text="@string/moonrise"
          android:textAlignment="center"
          android:textAppearance="@android:style/TextAppearance.Material.Menu"
          android:textColor="@color/lMoon"
          android:textSize="14sp" />
© www.soinside.com 2019 - 2024. All rights reserved.