Android — dark theme adaptation

When you turn on the dark mode, Android will search for the resources required by the dark mode in the resource directory, so you need to create a folder (xxxx-night) to store the dark mode resources

theme adaptation

To support a dark theme, you must set your app's theme (usually found in res/values/styles.xml) to inherit the DayNight theme:
<style name="AppTheme" parent="Theme.AppCompat.DayNight">

In addition you can use MaterialComponent's dark theme :
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

Example:
1. Create a values-night folder, and copy the themes.xml file under values ​​to values-night.
2. Add code to the themes.xml file in values-night:

  <style name="dark" parent="Theme.MaterialComponents.DayNight">
  </style>

3. Also add code to the themes.xml file in values:

  <style name="dark" parent="Theme.MaterialComponents.DayNight">
  </style>

4. Realize the theme according to the requirement

The difference between MaterialComponent and DayNight:

The MaterialComponent theme is a static dark theme, while the DayNight theme is a more dynamic theme that helps you easily switch between Light and Dark themes between apps.

Text color adaptation

There are two types of text color adaptation:
1. Through theme adaptation
2. Through color adaptation

Theme adaptation:

1. Create values-night, and copy the themes.xml file under values ​​to values-night.
2. Add code to the themes.xml file in values-night:

  <style name="dark_text" parent="Theme.MaterialComponents.DayNight">
        <item name="android:textColor">@color/white</item>
  </style>

3. Also add code to the themes.xml file in values:

  <style name="dark_text" parent="Theme.MaterialComponents.DayNight">
        <item name="android:textColor">@color/text_black</item>
  </style>

4. In the corresponding layout xml code:

 <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="1"
        android:maxLength="50"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="--"
        android:theme="@style/dark_text"
        android:textSize="14sp"
        android:textStyle="bold" />

Color adaptation:

1. Create values-night, and copy the colors.xml file under values ​​to values-night.
2. Add the code to the colors.xml file in values-night:

     <color name="dark_text">@color/white</color>

3. Also add the code to the colors.xml file in values:

     <color name="dark_text">@color/text_black</color>

4. In the corresponding layout xml code:

 <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="center"
        android:lines="1"
        android:maxLength="50"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="--"
        android:textColor="@color/dark_text"
        android:textSize="14sp"
        android:textStyle="bold" />

The displayed effect is that the text is displayed in black under the Light theme, and the text is displayed in white under the Dark theme.

shape adaptation

It is also very simple to adapt to the shape dark mode. You can create a shape file in drawable normally. You only need to pay attention to write the color required by the shape in values/colors.xml and values-night/colors.xml ( Same color fit as above).

picture adaptation

You need to create a drawable-night folder, just put the pictures in Dark mode into it, both png and svg can do this, if it is a png picture, you may also consider creating drawable-night-xxhdpi and other folders

at last

If you want to become an architect or want to break through the 20-30K salary range, then don't be limited to coding and business, but you must be able to select models, expand, and improve programming thinking. In addition, a good career plan is also very important, and the habit of learning is very important, but the most important thing is to be able to persevere. Any plan that cannot be implemented consistently is empty talk.

If you have no direction, here I would like to share with you a set of "Advanced Notes on the Eight Major Modules of Android" written by the senior architect of Ali, to help you organize the messy, scattered and fragmented knowledge systematically, so that you can systematically and efficiently Master the various knowledge points of Android development.
insert image description here
Compared with the fragmented content we usually read, the knowledge points of this note are more systematic, easier to understand and remember, and are arranged strictly according to the knowledge system.

Full set of video materials:

1. Interview collection

insert image description here
2. Source code analysis collection
insert image description here

3. The collection of open source frameworks
insert image description here
welcomes everyone to support with one click and three links. If you need the information in the article, directly scan the CSDN official certification WeChat card at the end of the article to get it for free↓↓↓

Guess you like

Origin blog.csdn.net/weixin_43440181/article/details/130249478