Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Styles, themes, and theme attributes


203

Styles, themes, and theme attributes


A style is an XML resource that contains attributes that describe how a widget should look and behave.
For example, the following is a style resource that configures a widget with a larger-than-normal text
size:



You can create your own styles (and you will in Chapter 22). You add them to a styles file in res/
values/ and refer to them in layouts like this: @style/my_own_style.


Take another look at the TextView widgets in fragment_crime.xml; each has a style attribute that
refers to a style created by Android. This particular style makes the TextViews look like list separators
and comes from the app’s theme. A theme is a collection of styles. Structurally, a theme is itself a style
resource whose attributes point to other style resources.


Android provides platform themes that your apps can use. When you created CriminalIntent, the
wizard set up a theme for the app that is referenced on the application tag in the manifest.


You can apply a style from the app’s theme to a widget using a theme attribute
reference. This is what you are doing in fragment_crime.xml when you use the value
?android:listSeparatorTextViewStyle.


In a theme attribute reference, you tell Android’s runtime resource manager, “Go to the app’s theme
and find the attribute named listSeparatorTextViewStyle. This attribute points to another style
resource. Put the value of that resource here.”


Every Android theme will include an attribute named listSeparatorTextViewStyle, but its definition
will be different depending on the overall look and feel of the particular theme. Using a theme attribute
reference ensures that the TextViews will have the correct look and feel for your app.


You will learn more about how styles and themes work in Chapter 22.


http://www.ebook3000.com

Free download pdf