开发者

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.

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 the开发者_如何学Gore is a single place where I can define all the colors which will override the current theme applied on the phone.

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.

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. Among other things it defines the default styles for widgets and values that control other parts of the look and feel for your 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. 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.

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.

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.


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
  • Some properties of AlertDialog (these can be changed using a custom dialog)
  • OS provided views such as the Quick Search Bar (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.

You can take a look at the theme the SDK uses:

  • themes.xml
  • styles.xml
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜