【问题标题】:Android : Theme.xml and Styles.xml?Android:Theme.xml 和 Styles.xml?
【发布时间】:2021-07-31 03:07:27
【问题描述】:

在较新版本的Android Studio中,看不到styles.xml,但是在values目录下,新增了两个XMLtheme.xmltheme.xml(night)

它是否真的取代了styles.xml的工作或者如果不同,它们之间有什么区别??

【问题讨论】:

  • 现在除了那个额外的夜间模式,它是一样的,只是命名更合适。实际上,values 文件的名称并不重要。他们都得到处理。拥有单独和命名文件的能力仅适用于程序员自己的组织。

标签: java android android-studio android-layout android-theme


【解决方案1】:

styles 是属性的集合,它定义了某个 ui 的外观 ex: editText 或 textView 一个主题同时决定了整个应用程序的外观(一个主题可以有多种样式取决于您的设计),白天和夜晚主题的区别在于让你的应用适应白天模式和夜间模式。因为现在在 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
    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

【讨论】:

    【解决方案2】:

    res\values 中使用 XML 属性实际上并不依赖于文件名,您可以在另一个文件(如 style.xml)中定义任何属性,如 &lt;string&gt;

    您甚至可以将style.xml 的名称更改为您喜欢的名称。

    在最近的 Android Studio 版本中,styles.xmlthemes.xml 取代,但后者提供了夜间模式的深色模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-27
      • 2013-02-16
      • 2017-09-19
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      相关资源
      最近更新 更多