简体   繁体   中英

Android styles and themes difference

According to the documantation: A theme is a style applied to an entire Activity or application, rather than an individual View .

But when I added theme to my view (a textview) in xml it was compiled and working. I thought that when I want to add to an individual View I need to use "style".

<TextView
android:id="@+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/TestTheme"
android:textSize="25dp"
android:text="TextView" />

Does anybody can explain to me?

A style is a collection of attributes that specify the look and format for a View. A style can specify attributes such as height, padding, font color, font size, background color, and much more.

For example, by using a style, you can take this layout XML:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />

And turn it into this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/CodeFont"
    android:text="@string/hello" />

The attributes related to style have been removed from the layout XML and put into a style definition called CodeFont , which is then applied using the android:textAppearance attribute.

To create the CodeFont style, save an XML file in the res/values/ directory of your project. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium">
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>

This example style can be referenced from an XML layout as @style/CodeFont, as illustrated in the TextView above. A theme IS a style . The only difference is a theme is a style applied to an entire Activity or app, rather than an individual View.

When a style is applied as a theme, every view in the activity or app applies each style attribute that it supports. For example, if you apply the same CodeFont style as a theme for an activity, then all text inside that activity appears in a green monospace font.

<resources>
    <style name="AppTheme" parent="Theme.Material">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
    </style>
</resources>

And the following example illustrates specifying values for the same attribute using references:

<resources>
    <style name="AppTheme" parent="Theme.Material">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorAccent">@color/accent</item>
    </style>
</resources>

To set a theme for all the activities of your app, open the AndroidManifest.xml file and edit the tag to include the android:theme attribute with the style name. For example:

<application android:theme="@style/CustomTheme">

If you want a theme applied to just one activity in your app, then add the android:theme attribute to the tag instead.

Just as Android provides other built-in resources, there are many pre- defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your activity appear like a dialog box:

<activity android:theme="@android:style/Theme.Dialog">

Or if you want the background to be transparent, use the Translucent theme:

<activity android:theme="@android:style/Theme.Translucent">

You need to create your widget styles and add that in your app theme and finally add your app theme in manifest. No need to mention the style in every view. like this Inside values folder in the styles file:

   <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textViewStyle">@style/TextViewStyle</item>
   </style>

   <style name="TextViewStyle" parent="android:Widget.TextView">
    <item name="android:background">@color/TextView_background</item>
    <item name="android:textColor">#16F20A</item>
    <item name="android:textStyle">bold</item>
   </style>


In Your manifest file  inside application tag add the below line.
    <application
      android:theme="@style/AppTheme">
    </application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM