Creating an Android project, the first and essential step, is to customize the application theme style. By using many of the system's properties to define the default style of the APP's various View, you can reduce many of the repeating property setting codes in the layout file. In the developer website R.attr, Google lists the system-level attributes in all Android SDKs. How to choose, how to distinguish, and what are the common attributes? This article is to enumerate some of the color-related properties introduced for reference.
Typically, in the application theme style defined in the Res/values/styles.xml file, the most common use combinations you might see are:
<resources> <!--Base application theme. <style name="Apptheme" parent="Theme.AppCompat.Light.NoActionBar" ><item name= "colorprimary" >@color/colorprimary</item> <item name="Colorprimarydark"> @color/colorprimarydark</item> <item name="color Accent "> @color/coloraccent</item> </style></Resources>
In fact, much more than that ...
Theme.appcompat
Since Lollipop, the Android system has introduced the Material design style, and the rendering styles of each control have changed significantly. To unify the UI style in different versions of the system, set the custom application Theme's parent style to Theme.appcompat series. Like the Theme.AppCompat.Light.NoActionBar subject here.
For APPCOMPAT related topics provide system properties, you can refer to the source code: V7/appcompat/res/values-v21/themes_base.xml.
Also, because of version compatibility issues with some properties, to avoid adding multiple versions of the styles file, you can omit the android: namespace.
Colorprimary
App Bar's background color, or ActionBar, is often a theme tint for an app. However, ActionBar has exited the historical stage and is used by Toolbar instead, but Toolbar needs to set the background color separately in the layout file using the Background property, such as:
<android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" />
Colorprimarydark
status bar background color. Only for Lollipop and later versions.
Coloraccent
Many controls use this color in either the selected state or the Get focus state, which is common:
- checkbox:checked status
- radiobutton:checked status
- switchcompat:checked status
- EditText: underline and cursor colors when getting focus
- Textinputlayout: Floating Label Font color
- Wait a minute
In addition to these three common control color properties, in fact, there are some color-related properties are also very useful.
Android:navigationbarcolor
Navigation bar background color. Only for Lollipop and later versions.
Colorcontrolnormal
Some views in the "normal" state of the color, common such as: unselected CheckBox and RadioButton, lose focus when the Edittext,toolbar overflow button color, and so on.
colorcontrolactivated
To some extent, it is a substitute for coloraccent, such as the checked state of the CheckBox and RadioButton, which overrides the corresponding color of the coloraccent attribute.
Colorcontrolhighlight
All clickable views Touch the Ripple (ripple) effect. Only for Lollipop and later versions.
Colorbuttonnormal
The background color of the Button normal state. Note that this setting differs from the button's android:background property by changing the background color, which in Lollipop and later causes the button to remain shaded and Ripple the touch effect.
Android:windowbackground
window background color, and so on are: android:background , and android:colorBackground so on.
Android:textcolorprimary
EditText text color, and so on.
And so on, there are many color-related system properties. Githubgist has a reference guide: the Android Lollipop Widget tinting guides, which lists the shading-related properties of commonly used controls.
Themeoverlay.appcompat
As the parent theme of application Theme, Theme.appcompat provides many properties to set the APP global views style. But sometimes, we still need to give one or some View settings a different style than the global style. In this case, Themeoverlay.appcompat will come in handy.
As the name implies, the Themeoverlay.appcompat series theme overrides the basic appcompat.theme style, changing only the style of the partial attribute as required. Here are some common uses:
Themeoverlay.appcompat
Inherit from @style/base.themeoverlay.appcompat. This is an empty topic, but it copies the related attributes in the AppCompat topic again. This is useful when you are setting a section style individually for individual View. As an example:
<style name="AppTheme.Secondary" parent="ThemeOverlay.AppCompat"> <item name="colorAccent">@color/colorPrimary</item></style>
Then use the android:theme attribute on a View in layout:
android:theme="@style/AppTheme.Secondary"
In this example, the Coloraccent property is overridden, and other properties are guaranteed to continue with the settings in the parent Theme.appcompat Apptheme. As you can see from the Themeoverlay.appcompat documentation, for example, the Colorprimary property is copied like this:
Android:colorprimary =? attr/colorprimary
If the parent uses Theme.appcompat or another theme, it is equivalent to setting the Coloraccent property only, and the other attributes use the default style.
ThemeOverlay.AppCompat.Light (Dark)
Unlike Themeoverlay.appcompat, this theme modifies the background color, text color, and highlight state color to match the Light (Dark) theme. For example, the white background in light style shows the Black (Dark) text, and the White (light) text appears in the black background under the Dark style.
The use of this theme can be the case, such as our global theme is light, but some of the UI may need to use a Dark style background, in this case, each View set color alone is more troublesome, can be set in ViewGroup unified, such as:
<FrameLayout android:background=”@color/dark_background” android:theme="@style/ThemeOverlay.AppCompat.Dark”> <TextView /></FrameLayout>
ThemeOverlay.AppCompat.Dark.ActionBar
ThemeOverlay.AppCompat.Light.ActionBar
By comparison, the theme is used more widely because of the widespread use of Toolbar. As we said earlier, both the Colorcontrolnormal and Textcolorprimary properties can affect the overflow button color and overflow text color of Toolbar or ActionBar, and also change the display color of other View. In a comprehensive consideration, we will not use these properties in application Theme to control the content color of the Toolbar, but instead use the Theme attribute that is set individually to Toolbar for the topic.
For example, the global use of light theme, Toolbar background color is blue or red color, require the overflow button color is white. If there is no special setting, the overflow button is displayed as black by default. At this point, you can use this topic:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:background="?attr/colorPrimary"></android.support.v7.widget.Toolbar>
Summarize
System themes provide a lot of property styles, but also affect each other, many-to-many relationships, complex, and some there will be version compatibility issues, use must be more testing. Of course, using the appropriate words, you can reduce the number of repetitive XML code, but also facilitate the subsequent application theme changes in the need to expand such scenarios.
About me: also maple, blog address: http://yifeng.studio/, Sina Weibo: it also maple
Scan QR Code, Welcome to my personal public number: Android note-taker
Not only share my original technical articles, but also the programmer's career reverie
In Android Theme.appcompat, you should be familiar with the color properties