首页 > 解决方案 > Android:Theme.xml 和 Styles.xml?

问题描述

在较新版本的 Android Studio 中,我看不到,但是在 values 目录中,styles.xml有两个新的XML和.theme.xmltheme.xml(night)

它实际上是否取代了styles.xml OR如果不同的工作,它们之间有什么区别?

标签: javaandroidandroid-studioandroid-layoutandroid-theme

解决方案


样式是属性的集合,它定义了某个 ui 的外观例如:editText 或 textView一个主题同时决定了整个应用程序的外观(单个主题可以有多种样式取决于您的设计),白天和晚上主题之间的区别就是让你的app适应白天模式和夜间模式。因为现在在 android OS 中,您可以强制使用暗模式。

作为开发人员,您需要为此准备好您的应用程序。

如何使用这个

为项目中标记的两个主题赋予不同的颜色(以下来自主题文件)

  <resources xmlns:tools="http://schemas.android.com/tools">
   <!-- Base application theme. -->
   <style name="Theme.<Your app name>" <---- this is autogenrated this wil be the id to be used
   parent="Theme.MaterialComponents.DayNight">
     </style>
     </resources>

要在 textview 或按钮或布局文件中使用,只需:

    <TextView
    android:id="@+id/tv_StackOverflow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Theme.<Your app name>" /> <---insert your style name inside the theme.

现在,当用户在手机的开发者模式下强制打开暗模式时,您的应用程序将立即适应它并更改其 UI 的颜色。

更多信息可以在这里找到 - https://developer.android.com/guide/topics/ui/look-and-feel/themes


推荐阅读