在所有Android设备上一致的用户界面颜色上一、用户界面、颜色、设备

2023-09-07 13:47:28 作者:-余存

我注意到了UI颜色(如按钮背景/文字颜色),从设备到设备的所有变化的基础上,即在一个设备正在使用的当前主题。

I noticed the UI color (eg Button background/text color) all changes from device to device, based on the current theme that is being used in a device.

什么是最好的做法,以应用自定义的UI颜色Android应用程序,让我有相同的配色方案为我所有的Andr​​oid设备的应用程序。我可以设置一个UI项文本/背景颜色。我想知道,如果有,我可以定义所有这些都将覆盖应用在手机上的当前主题颜色单一的地方。

What is the best practice to apply custom UI colors for Android app, so that I have same color scheme for my app in all Android devices. I can set text/background color on a UI item. I'm wondering if there is a single place where I can define all the colors which will override the current theme applied on the phone.

THX。

推荐答案

是的,有在这里你可以为你的应用程序定义这些值的一个地方。请参见样式和主题的在Android文档它是如何工作的。

Yes, there is a single place where you can define these values for your app. See Styles and Themes in the Android docs for how it works.

一个风格值predefined名字只是一个映射。如果你发现自己重复了一些在你的布局中共同的属性,你可以将这出成风​​格。例如,你可能有一个特殊的按钮样式,定义一个特定的背景和文字颜色。

A style is just a mapping of values to predefined names. If you find yourself repeating a number of common attributes in your layouts, you can factor that out into a style. For example, you might have a special button style that defines a specific background and text color.

一个主题是一种荟萃的风格。它可以应用到通过您的Andr​​oidManifest.xml一个活动或甚至整个应用程序。除此之外它定义了默认样式部件和值控制的外观其他部分和感觉你的用户界面。

A theme is a sort of meta-style. It can be applied to an Activity or even a whole application through your AndroidManifest.xml. Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your UI.

当你试图融入系统中,否则自定义用户界面为你的应用程序,你可以查询当前主题值。就像你使用 @ 引用语法 @android:绘制/富指的是系统资源时,可以使用语法安卓?ATTR /富当你想使用存储在系统主题属性的值

When you're trying to blend in with the system in an otherwise custom UI for your app, you can query the current theme for values. Just like you use the @ reference syntax @android:drawable/foo when referring to a system resource, you can use the syntax ?android:attr/foo when you want to use the value stored in the system theme attribute foo.

在你的情况,如果你想改变的主要文本颜色在您的应用程序,应用自定义主题,设置属性 textColorPrimary 。如果你只是想确保你的应用程序的一个元素是由您的应用程序上运行的设备定义的主要文本颜色,你可以设置安卓文字颜色=机器人:ATTR / textColorPrimary 。同样的原则也适用于其他地方也是如此。

In your case, if you want to change the primary text color across your app, apply a custom theme that sets the attribute textColorPrimary. If you just want to be sure that an element of your app is using the primary text color as defined by the device your app is running on, you can set android:textColor="?android:attr/textColorPrimary". The same principles apply elsewhere as well.

如果你想看到什么属性在系统中使用,它们被定义为在该文件中的Andr​​oid框架的一部分:frameworks/base/core/res/res/values/attrs.xml.看看XML元素的子元素<申报,设置样式名称=主题> 上方。要查看一下系统设置这些来,看themes.xml在同一目录下。最后,并非所有的这些属性都是公开的 - 非公有制属性无法通过一个应用程序进行设置,他们是Android框架的实施细节。请参阅public.xml对于这些属性的完整列表,可用于在应用程序中使用。

If you want to see what attributes are used in the system, they are defined as part of the Android framework in this file: frameworks/base/core/res/res/values/attrs.xml. Look at the children of the XML element <declare-styleable name="Theme"> at the top. To see examples of what the system sets these to, see themes.xml in the same directory. Finally, not all of these attributes are public - non-public attributes cannot be set by an app, they're implementation details of the Android framework. See public.xml for the complete list of which attributes are available for use in apps.