官术网_书友最值得收藏!

Applying local styles and global themes

Themes are Android's mechanism for applying a consistent style to an app or activity. A style specifies the visual properties of the elements that make up your user interface, such as color, height, padding, and font size.

How to do it...

  1. We can create styles for views using the various attributes:
    <TextView
      style="@android:style/TextAppearance.Medium"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textColor="#7F3300"
      android:typeface="monospace"
      android:gravity="center"
      android:text="Inline Styles" />
  2. These styles can be extracted into a separate file so that they can be reused across views, as with string resources. In this case, we inherit from the Android style TextAppearance.Medium, but this is not always necessary. Let's take a look at the following code snippet:
    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
      <style name="MyStyle"
        parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#7F3300</item>
        <item name="android:typeface">monospace</item>
        <item name="android:gravity">center</item>
      </style>
    </resources>
  3. Using styles in a layout resource is done by using the style attribute:
    <TextView style="@style/MyStyle" android:text="Styled" />
  4. This can also be done for the actual activities, but in this case, we inherit from an activity style, Theme.Light:
    <?xml version="1.0" encoding="UTF-8"?>
    <resources>
      <color name="mycolor">#007F0E</color>
      <style name="MyTheme" parent="android:Theme.Light">
        <item name="android:windowBackground">@color/mycolor </item>
        <item name="android:colorBackground">@color/mycolor</item>
      </style>
    </resources>

How it works...

A style is a collection of properties that specify the look and format for a view or window. A theme is a style applied to an entire activity or application, rather than an individual view.

Note

Styles and themes are saved in the values resource folder with the .xml extension.

Styles can be inherited and extended, meaning we can take an existing style and add a few additional attributes and customizations. Inheritance is achieved by using the parent XML attribute on the <style> element.

There is also a shortcut for inheriting our own styles by using a special naming system. If we were to create a style named MyStyle.Bigger, this new style would inherit all the attributes from the MyStyle style. This only applies to local styles and not Android styles.

Due to inheritance and the ability to have same-name resources in different folders, we can create advanced styling that is based on various device configuration options, such as orientation, screen size, or Android version. For example, we could have a larger padding for larger screens and use new features on newer devices.

主站蜘蛛池模板: 泸州市| 栖霞市| 黑山县| 郓城县| 临安市| 曲沃县| 改则县| 铜川市| 如皋市| 金秀| 项城市| 绥中县| 东乌珠穆沁旗| 鄂托克旗| 胶南市| 宝丰县| 洛隆县| 崇明县| 浦县| 石河子市| 乌拉特后旗| 神农架林区| 油尖旺区| 霸州市| 开鲁县| 中阳县| 白银市| 武陟县| 忻城县| 会东县| 永平县| 昆明市| 永仁县| 阳朔县| 黄梅县| 蓬莱市| 鄂伦春自治旗| 葫芦岛市| 汽车| 云阳县| 镇江市|