More on Layout Attributes

Now you are going to add a few more tweaks to the design of list_item_crime.xml and, in the process, answer some lingering questions you might have about widgets and attributes.

Navigate back to the design view of list_item_crime.xml. Select crime_title and adjust some of its attributes: Expand the textAppearance section and set the textSize attribute to 18sp. Set the textColor attribute to @android:color/black (Figure 10.32).

Figure 10.32  Updating the title color and size

Updating the title color and size

You can type these values directly into the fields in the attributes pane, choose from the dropdown (in the case of textSize), or click the ... button next to the field to pick a dimension resource.

Run CriminalIntent and be amazed at how much better everything looks with a fresh coat of paint (Figure 10.33).

Figure 10.33  Fresh paint

Fresh paint

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:

      <style name="BigTextStyle">
        <item name="android:textSize">20sp</item>
        <item name="android:padding">3dp</item>
      </style>

You can create your own styles (and you will in Chapter 21). 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 layout/fragment_crime.xml (not the list_item_crime.xml file you have been working in for this chapter). 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 21.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.238.57.9