Android style summary

About the author: CSDN content partner and technical expert, built an APP with tens of millions of daily users from scratch.
Focus on sharing original series of articles in various fields, specializing in java backend, mobile development, business realization, artificial intelligence, etc. I hope everyone will support me.

Insert image description here

1. Introduction

We continue to summarize and studyAndroid basic knowledge, review the past and learn the new.

This article talks about style-related knowledge.

2. Overview

Styles define the format and appearance of the interface. Styles can be applied to a single View (from a layout file) or to an entire activity or app (from a manifest file).
A style is a simple resource referenced using the value provided in the name attribute (not the name of the XML file). Therefore, it is possible to combine style resources with other simple resources under a single element in an XML file.

A style is a collection of properties that specify the appearance of an individual View. Styles can specify attributes such as font color, font size, and background color.

Themes and styles have many similarities, but their uses are different. We will explain them in detail later.

3. Use

All style files are defined in the following directory:

res/values/filename.xml

文件名可以任意设置。元素的 name 将用作资源 ID
该文件通常命名为 styles.xml

grammar

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style
        name="style_name"                                  样式的名称
        parent="@[package:]style/style_to_inherit">        样式资源。对此样式应从中继承样式属性的样式的引用
        <item                                              定义样式的单个属性。必须是 <style> 元素的子元素
            name="[package:]style_property_name" 
            >style_value</item>
    </style>
</resources>

The purpose of designing style is to maintain a unified format and presentation effect while reducing maintenance costs in the use of complex multi-style controls and project applications.

3.1 Create and apply styles

Open the project's res/values/styles.xml file and follow these steps:

  1. Add a name that uniquely identifies the style

eg:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CustomText" parent="@style/Text">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#008</item>
    </style>
</resources>

use

<?xml version="1.0" encoding="utf-8"?>
<EditText
    style="@style/CustomText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, World!" />

You must know that our Style style can be inherited. You can specify the style inherited by the newly defined style through the parent attribute in the style tag. The parent inherits some styles of the system. Inheritance is optional. You can choose to inherit or not.

3.2 Create and apply themes

You can create a theme background just like you create a style. It is exactly the same, except that the location we use is different. The theme is mainly applied on the ** mark or **,
Use android:theme Properties to reference, then each view in the application or activity will apply the styles defined in the specified theme

<manifest ... >
    <application android:theme="@style/Theme.AppCompat" ... >
    
        <activity android:theme="@style/Theme.AppCompat.Light" ... >
        
    </application>
</manifest>


Android provides several ways to set properties throughout your Android application. For example, you can set properties directly in the layout, apply styles to views, apply themes to layouts, and set properties programmatically.
So these settings are prioritized, and the following list will determine which attributes are ultimately applied. The list is sorted from highest to lowest priority:

  1. Apply character or paragraph-level styles to a TextView-derived class via a text span
  2. Apply properties programmatically
  3. Apply individual properties directly to the View
  4. Apply styles to View
  5. Default style
  6. Apply a theme to a View collection, activity, or entire app
  7. Apply some View-specific styles, such as setting TextAppearance for a TextView

3.3? & @ symbol reference

Let's look at an example first. It's also a reference to color. One uses @ and the other uses ? , so what is the difference between them?


    <TextView
        android:textColor="@color/standard_font" />

    <TextView
        android:textColor="?attr/darkColor_ffeaeaea"/>

"@" indicates a resource reference, stating that this is a resource reference, the format is: @[package:]type/name

"?" means referencing the theme attribute, the format is: ?[namespace:]type/name
The question mark means: this attribute is an attribute defined by the current Theme, so the system will Find its parameters in the current Theme.
Let’s analyze it with the above example,

  1. Find the application theme from AndroidManifest.xml: android:theme="@style/AppTheme"
  2. The darkColor_ffeaeaea attribute can be found in the AppTheme theme:
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
……
        <item name="darkColor_ffeaeaea">@color/colorPrimary</item>
……

If it is an attribute defined within the application, [namespace:]type can be omitted,
such as:

android:background=“?attr/colorPrimary”

It can be abbreviated as:

android:background=“?colorPrimary”

If a system-defined attribute is referenced, it cannot be omitted:

android:background=“?android:attr/colorPrimary”

style

5. Recommended reading

Java column

SQL Column

Data Structures and Algorithms

Android learning column

ddd

Guess you like

Origin blog.csdn.net/fumeidonga/article/details/134903857