简体   繁体   English

Android应用程序主题覆盖视图主题

[英]Android application Theme overriding view theme

I have been developing an Android app for quite some time now and almost everything is how it's supposed to be. 我已经开发Android应用程序已有一段时间了,几乎所有事情都应该如此。 However, I'm having some issues with themes for some views. 但是,对于某些观点,我在主题方面遇到了一些问题。 My application uses a AppCompat.Light based theme as shown bellow: 我的应用程序使用基于AppCompat.Light的主题,如下所示:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar"></style>

Everything looks white and clean, but I have one specific screen that is kind dark. 一切看上去都很白净,但我有一个特定的屏幕有点暗。 So I created the following theme for some EditText fields: 因此,我为一些EditText字段创建了以下主题:

<style name="InputField" parent="Theme.AppCompat"></style>

in order to make the input fields have white colors. 为了使输入字段具有白色。 I then run the application on devices with different Android versions. 然后,我在具有不同Android版本的设备上运行该应用程序。 On a smartphone with Android 5.0.2, everything looks as I expected. 在装有Android 5.0.2的智能手机上,一切看起来都与我预期的一样。 On a smartphone with android 4.4.4, however, the input is dark. 但是,在具有Android 4.4.4的智能手机上,输入为暗。 I even tried to change the theme of the input field theme to 我什至尝试将输入字段主题的主题更改为

<style name="InputField" parent="Theme.Holo"></style>

or 要么

<style name="InputField" parent="@android:style/Widget.Holo.EditText"></style>

in the values-v19 styles, but it still looks dark. values-v19样式中,但它仍然看起来很暗。 If I change the application theme to 如果我将应用程序主题更改为

<style name="AppBaseTheme" parent="Theme.AppCompat.NoActionBar"></style>

the input fields look as they should, but the entire rest of the app is dark themed and it's not what I want. 输入字段看起来应该是应该的,但是应用程序的其余部分都是深色主题,这不是我想要的。 I can only assume that application theme is having some influence over my views theme, even though I explicitly specified the theme they should use. 我只能假定应用程序主题对我的视图主题有一定的影响,即使我明确指定了它们应该使用的主题。 The same behavior happens on a smartphone with android 4.2.2. 在具有Android 4.2.2的智能手机上也会发生相同的行为。

System provided styles doesn't define used colors, they have only references to a theme attributes. 系统提供的样式未定义使用的颜色,它们仅引用主题属性。 Colors are defined the theme, thus, it not enough, if you set parent to some system style. 颜色是主题的定义,因此,如果将parent设置为某种系统样式,则颜色是不够的。

Starting API 21 (5.0) you can override theme for specific views (or styles) by setting attribute android:theme . 从API 21(5.0)开始,您可以通过设置属性android:theme来覆盖特定视图(或样式)的android:theme For older versions you will need to define specific colors and drawables in your style. 对于较旧的版本,您需要在样式中定义特定的颜色和可绘制对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM