长话短说,我有一个应用程序,我在其中直接将样式属性应用于活动和片段的 XML 文件。现在我将其重构为styles.xml
文件。我几乎不敢直接修改主题本身,因为我之前已经尝试过类似的东西,继承变得混乱。
所以,我的作品里有好几种这样的风格styles.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<style name="My_DarkMode" parent="Theme.MyApp">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
</style>
<style name="My_DarkMode.MaterialToolbar">
<item name="android:background">@color/purple_500</item>
</style>
<style name="My_DarkMode.TextView">
<item name="android:textColor">@color/white</item>
</style>
<style name="My_DarkMode.HighlightedText">
<item name="android:textColor">@color/yellow_4text</item>
</style>
<style name="My_DarkMode.TabLayout">
<item name="android:background">@color/tab_grey</item>
<item name="tabIndicatorColor">@color/tab_indicator</item>
<item name="tabSelectedTextColor">@color/tab_selected_text</item>
<item name="tabTextColor">@color/tab_text</item>
</style>
<style name="My_DarkMode.FragmentBg">
<item name="android:background">@color/black</item>
</style>
<style name="My_DarkMode.MainBg">
<item name="android:background">@color/activity_grey</item>
</style>
<style name="My_DarkMode.SearchBar">
<item name="android:background">@color/white</item>
<item name="android:textColor">@color/black</item>
</style>
<!-- etcetera... -->
因此,我用活动和片段内的 XML 对象中的这些样式之一替换了硬编码属性。这很好用。 (请注意,不同的 XML“视图对象”可能使用所有这些样式中的一种或另一种)。
那么问题来了:
因此,让我们想象一下,我采用所有这些样式并复制其中的每一种样式,但使用不同的名称:而不是命名它们My_DarkMode
or My_DarkMode.*
,我给他们命名My_LightMode
,并完全更改这些“LightMode 样式”的调色板(因此,将它们用作某种主题)。
所以,这个想法将以编程方式切换:
<!-- It's a little pseudo-code, for summarizing the thing -->
<TextView
style="@style/My_DarkMode.TextView"
android:id="[...]" />
<TextView
style="@style/My_DarkMode.HighligthedText"
android:id="[...]" />
<!-- [...] -->
...到此(但对于比这更多的 XML 元素):
<TextView
style="@style/My_LightMode.TextView"
android:id="[...]" />
<TextView
style="@style/My_LightMode.HighlightedText"
android:id="[...]" />
<!-- [...] -->
通过Java(你知道,通过单击某个按钮或类似的东西:我不关心如何触发这个问题,而是我应该使用哪些指令),切换样式的正确方法是什么要施加?我不能以某种方式将所有这些样式分组到一个主题或其他东西中吗?看来我别无选择,只能做类似的事情,因为如果我从主题应用其中的一些内容并让继承完成它的工作,那么 SearchView 就会继承白色字母(带有白色背景;所以你可以明白我的意思混乱)...而且,我对 Android 风格和主题的了解确实有限。
作为记录
我知道我的这种方法可能根本不是一个好的方法。除此之外:因为我对 Android 中的样式、主题和样式继承层次结构如何工作一无所知。 (我的意思是,如果我保留这种方法,也许我的 XML 代码会太湿?我想知道......)如果是这样,欢迎使用更简洁的方法。
如果这是 HTML 和 CSS,出于某种原因我将能够更容易地解决这个问题,或者我认为......
EDIT:说实话:无论我读了多少文档,比如Themes or 主题主颜色属性如何工作……读得越多,我就越困惑。尝试和错误也没有多大帮助......最近我尝试从 XML 布局中删除所有样式并从头开始主题;并尝试它是什么在这个链接里面我发现如果我这样做:
<item name="colorBackground">@color/purple_500</item>
...colorBackground
只是不存在!无数次,有些东西不像文档中那样工作......叹息......
所以这里是:“从头开始的主题”。忘记所有style=
我之前设置的:我把它们全部删除了。
My themes.xml
:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- all these ones were before, I started tweaking
them and see what happens -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/white</item>
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- <item name="toolbarStyle">@style/My_DarkMode.MaterialToolbar</item> -->
<item name="toolbarStyle">@style/Widget.MaterialComponents.Toolbar.PrimarySurface</item>
<item name="drawerArrowStyle">@style/Theme.MyApp.DrawerArrowStyle</item>
<!-- and here I add new ones I din't knew before
and start playing with them like crazy -->
<item name="colorSurface">@color/activity_grey</item>
<item name="colorOnSurface">@color/white</item>
<item name="colorOnPrimarySurface">@color/yellow_4text</item>
<item name="colorContainer">@color/activity_grey</item>
<item name="colorOnBackground">@color/yellow_4text</item>
<!-- It doesn't exist... sigh... vVv -->
<!-- <item name="colorBackground">@color/black</item> -->
</style>
我很难想象、发现或达到哪些通用主题属性可以调整一些琐碎的事情,比如让所有 TextViews 显示具有相同颜色的文本,或者为什么colorSurface
or colorContainer
漆成灰色<NavigationView>
,但不是<ConstraintLayout>
的背景??我试图保持一切与以前相似,但有一个集中的主题,我可以通过编程从一个地方进行切换。
我在“Android 开发人员”中阅读了很多有关样式和主题以及它们应该如何协同工作的链接......但它们没有清楚地显示如何操作!如果有一种规范的方法可以在一个主题中加入多种样式,那就太好了或类似的东西...所以,您可以通过切换主题将不同的样式应用于不同的视图,就像它或多或少与 CSS 类一起工作一样。
Old:
(before wanting to extract a theme)
新的混乱:
(removed styles from Layouts and started modifying Theme from scratch)