简体   繁体   English

所有 Android 设备中的 UI 颜色一致

[英]Consistent UI color in all Android devices

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 颜色(例如按钮背景/文本颜色)会根据设备中使用的当前主题在设备之间发生变化。

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.为 Android 应用程序应用自定义 UI colors 的最佳实践是什么,以便我在所有 Android 设备中的应用程序具有相同的配色方案。 I can set text/background color on a UI item.我可以在 UI 项上设置文本/背景颜色。 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.我想知道是否有一个地方可以定义所有 colors 将覆盖手机上应用的当前主题。

thx.谢谢。

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.有关其工作原理,请参阅 Android 文档中的Styles 和主题

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.例如,您可能有一个特殊的按钮样式,它定义了特定的背景和文本颜色。

A theme is a sort of meta-style.主题是一种元风格。 It can be applied to an Activity or even a whole application through your AndroidManifest.xml.它可以通过您的 AndroidManifest.xml 应用于一个 Activity 甚至整个应用程序。 Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your UI.除其他外,它定义了默认的 styles 用于控制 UI 外观和感觉其他部分的小部件和值。

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.当您尝试在应用程序的其他自定义 UI 中与系统融合时,您可以查询当前主题的值。 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 .就像在引用系统资源时使用@引用语法@android:drawable/foo一样,当您想使用存储在系统主题属性foo中的值时,可以使用语法?android:attr/foo foo 。

In your case, if you want to change the primary text color across your app, apply a custom theme that sets the attribute textColorPrimary .在您的情况下,如果您想更改整个应用程序的主要文本颜色,请应用设置属性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" .如果您只是想确保您的应用程序的元素使用由您的应用程序运行的设备定义的主要文本颜色,您可以设置android:textColor="?android:attr/textColorPrimary" The same principles apply elsewhere as well.同样的原则也适用于其他地方。

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 .如果您想查看系统中使用了哪些属性,它们在此文件中定义为 Android 框架的一部分: frameworks/base/core/res/res/values/attrs.xml Look at the children of the XML element <declare-styleable name="Theme"> at the top.查看顶部的 XML 元素<declare-styleable name="Theme">的子元素。 To see examples of what the system sets these to, see themes.xml in the same directory.要查看系统将这些设置为的示例,请参阅同一目录中的theme.xml 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.最后,并非所有这些属性都是公共的——非公共属性不能由应用程序设置,它们是 Android 框架的实现细节。 See public.xml for the complete list of which attributes are available for use in apps.有关可在应用程序中使用的属性的完整列表,请参阅public.xml

Best practice is to apply a custom theme to your application, and override as much of the default properties as you need.最佳实践是将自定义主题应用于您的应用程序,并根据需要覆盖尽可能多的默认属性。

Almost everything can be changed, except几乎所有东西都可以改变,除了

  • The Menu Menu
  • Some properties of AlertDialog (these can be changed using a custom dialog) AlertDialog的一些属性(可以使用自定义对话框更改这些属性)
  • OS provided views such as the Quick Search Bar (QSB)操作系统提供的视图,例如快速搜索栏 (QSB)

If you like the look of the default SDK resources then you can find these in sdk_folder/platforms/android-9/data/res/ (replace 9 with the SDK version you want the resources from) - copy the ones you want into your App and reference those.如果您喜欢默认 SDK 资源的外观,那么您可以在sdk_folder/platforms/android-9/data/res/中找到这些(将9替换为您想要的资源的 SDK 版本) - 将您想要的资源复制到您的应用程序中并参考那些。

You can take a look at the theme the SDK uses:您可以查看 SDK 使用的主题:

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

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