Android Studio Theme Style in the Detailed Explanation

  • 2021-11-24 03:00:01
  • OfStack

1. Theme

A theme is a collection of one or more formatting properties. Calling the theme resource in a program can change the style of the form, which has a global impact on the whole application or an Activity.

Define location: res/values In the styles. xml file under the directory

Label

< style > < /style > : Define a topic

< item > < /item > Styling Themes

Example (defining a topic named AppTheme)


 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <!-- Customize your theme here. -->
      <item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
 </style>

Use of themes

(1) In the XML file: android: theme = "@ style/AppTheme"

(2) In the code of Java: setTheme (R. style. AppTheme);

2. Style

Style: Set the specific style of View.

Definition location: styles. xml file under res/values directory

Label

< style > < /style > : Defining Styles

< item > < /item > Set the style of the control

Invoking a method in an XML file

style="@style/textViewSytle"

3. Custom Styles

When the custom style or theme cannot meet the requirements, you can also customize the style. The steps for customizing the style and theme are as follows:

1) Create 1 style file style. xml in the res/values directory and add 1 <resources> Root node.

2) In <resources>节 Add 1 to the point < style > Node, and define a only 1 name for the style or theme in this node, or optionally add a parent class attribute to indicate that the current style inherits the style of the parent class.

3) In < style > 1 or more are declared in the node < item > , each < item > The node needs to define a property name and set the value of this property inside the element.

Summarize

(1) Style:

Styles in Android are similar to CSS styles in that they are used to define display styles for interface elements and are a collection of one or more View control properties. Styles can only act on a single View, such as EditText and TextView. Using styles, you can specify the duplicate attributes of multiple controls and extract them for writing, so as to avoid writing a large number of duplicate codes.

(2) Theme:

A topic is also a collection of one or more View control properties, but it has a different scope. Topics in AndroidManifest. xml < application > And < activity > Node is used in the whole application or an Activity, and its influence is global. If a theme is used in an application and a style is also used in the View under the application, the style takes precedence over the theme when there is a conflict between the theme and the attributes in the style.

In the Android system, your own styles and themes can be used directly. For example, setting themes can be done through android:theme="android:style/…"。


Related articles: