Bar*_*fet 3 android android-theme android-view android-styles
我对这个话题有一些疑问。
我只使用 android 为我们提供的 API 的夜间/白天主题,您可以为两者设置主题。然后它适用于 setNightMode() 和类似的东西。然后我所做的是在视图背景中设置一种颜色,当设置为夜间主题时它会发生变化
所以我想知道我是否像这样创建 3 个主题(浅色/深色/蓝色)(我不想重复代码,所以我将显示 1 个主题):
<style name="Theme.Blue" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryVariant">@color/greentwo</item>
<item name="colorOnPrimary">@color/reed</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
Run Code Online (Sandbox Code Playgroud)
然后当我进入活动/片段时:
我检查用户设置的首选主题是什么,例如看到的主题是Theme.Blue
因此,对于这个主题,我想加载一些绿色文本和其他红色文本,所以我的想法是以编程方式设置主题。TextView.setAppareance(Theme style)这似乎是一项非常艰苦的工作。
同样的情况也适用于背景,其中存在相同颜色的较高和较低色调的对比等等。
我是否必须继承任何主题,将其设置在 xml 的顶部或将其 1 by 1 设置到每个视图或我不知道的任何内容?
那么我该怎么做呢,推荐的方法是什么?
我不太明白 colorPrimary、colorPrimaryVariant 之类的东西去哪里了。例如,当创建警报对话框时,按钮设置为绿色!
关于管理当前用户首选项主题不是问题,问题在于将其应用到所有视图的样式。
拥有不同主题的全部意义在于不必以编程方式更改所有内容。所以你需要针对不同的主题有不同的风格:
<resources>
<!-- Base application theme. -->
<!-- Light theme. -->
<style name="LightMode" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary2</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark2</item>
<item name="colorAccent">@color/colorAccent2</item>
<item name="color_text">@color/color_text2</item>
</style>
<!-- Dark theme. -->
<style name="NightModeTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="color_text">@color/color_text</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
以及您的不同属性attr.xml:
<resources>
<declare-styleable name="Theme">
<attr name="colorPrimary" format="color"/>
<attr name="colorPrimaryDark" format="color"/>
<attr name="colorAccent" format="color"/>
<attr name="color_text" format="color"/>
...
</declare-styleable>
</resources
Run Code Online (Sandbox Code Playgroud)
在您的中设置不同的颜色,如下所示colors.xml:
<resources>
<!-- Dark theme colors. -->
<color name="colorPrimary">#353535</color>
<color name="colorPrimaryDark">#252525</color>
<color name="colorAccent">#FF3C00</color>
<color name="color_text">#FFFFFF</color>
<!-- Light theme colors. -->
<color name="colorPrimary2">#FFFFFF</color>
<color name="colorPrimaryDark2">#FFBB29</color>
<color name="colorAccent2">#FFB728</color>
<color name="color_text2">#272727</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
attr然后在 xml 文件的视图中,您将从文件而不是文件中获取颜色,colors如下所示:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read me"
android:textColor="?attr/color_text"/>
Run Code Online (Sandbox Code Playgroud)
此外,当您想为每个活动设置主题时,请确保在设置内容视图之前执行此操作。
你可以有这样的课程:
abstract class ThemeManager {
public static void set(Context context, String activeTheme) {
int themeRecourseID = R.style.LightMode;
if (activeTheme.equals("DarkMode")) {
themeRecourseID = R.style.DarkMode;
}
context.setTheme(themeRecourseID);
}
}
Run Code Online (Sandbox Code Playgroud)
并以这种方式使用它:
override fun onCreate(savedInstanceState: Bundle?) {
//SET THE THEME HERE BEFORE SETTING THE CONTENTVIEW OF THE ACTIVITY
ThemeManager.set(this, "DarkMode");
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2061 次 |
| 最近记录: |