"Android" 8.4 lets the theme adapt to different Android versions

Source: Internet
Author: User
Tags custom name

Category: C #, Android, VS2015;

Date Created: 2016-02-17 I. INTRODUCTION

By default, themes that are provided by a higher version cannot be run on a low-version Android system. However, with custom themes, you can adapt your system to their respective versions. The biggest benefit of this approach is that if you just want to adapt your application to different Android systems, you need to design a single application without having to implement different versions of the application for different Android systems. Second, example-ch0804themedemo

1. Operation

Is the effect of the main interface of this project running under the Android 6.0 (API 23) Simulator:

Do not change any options, but select the emulator as the x86 emulator for API 19, and then press <F5> Debug to run, at which point it automatically uses the Style.xml file under the Resources/values folder.

The image below is the effect of the main interface of this project running under the Android 4.4.2 (API 19) emulator, and the image on the right is the effect of this example running:

2. Design steps

For example, the material theme is provided from Android 5.0 (V21), which cannot be used in Android 4.4 (v19) that is lower than Android 5.0, but you do not want to create an entirely functionally identical project just to solve the topic. That is, if you want your application to be able to adapt to the Android system you are running without any changes, you need to implement the theme compatibility in your project by following the steps below.

(1) Under the Resources folder, add subfolders and styles.xml that correspond to the version

Create a VALUES-V21 subfolder under the Resources folder, and then create a styles.xml file under the subfolder:

<?  ?><resources>  <name = "MyTheme" Parent = "Android:Theme.Material.Light.DarkActionBar" >  </ style >
  ... </ Resources >

VALUES-V21 indicates that the Style.xml file under this folder applies only to Android 5.0 and later.

(2) Add the lower version of the Styles.xml under the Resources/values folder

Modify the Resources/values/styles.xml file created by the previous example to inherit from the previous version of the theme, with the same name as the custom name above:

<?  ?><resources>  <name = "MyTheme" Parent = "Android:Theme.DeviceDefault.Light.DarkActionBar" >  </ style >
  ... </ Resources >

With the two steps above, you can implement version resilience with a reference to custom themes. This is because when the application is running on Android 5.0 and later devices, the theme specified in the resources/values-v21/styles.xml is automatically used; When the app is below Android 5.0 of devices running on the device (such as API 19), the system automatically uses the custom theme specified in Resources/values/styles.xml.

(3) referencing a custom theme

To use a theme named "MyTheme" at the application level, you only need to modify the configuration of the Androidmanifest.xml file:

<application android:label= "MyApp" android:theme= "@style/mytheme" >

</application>

To use a custom theme on an active page, simply declare it in the corresponding activity:

[Activity (Label = "MyActivity", Theme = "@style/mytheme")]

Note: If your custom theme uses a custom color in the Colors.xml file, be sure to place the custom Colors.xml file under the Resources/values folder instead of the Resources/values-v21 folder , the purpose of this is to make all versions accessible to custom colors. If placed under the Resources/values-v21 folder, only Android 5.0 (API 21) and later can access custom colors.

3. The source file corresponding to this example

(1) Ch0804_themedemo.axml file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:text= "Hello, this is the page referencing the Mythemes custom theme." "android:textappearance= "? Android:attr/textappearancelarge"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:id= "@+id/textview1"android:gravity= "Center" /></LinearLayout>

(2) Ch0804ThemeDemo.cs file

usingAndroid.app;usingAndroid.os;namespacemydemos.srcdemos{[Activity (Label="@string/applicationname")]     Public classch0804themedemo4:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate);        Setcontentview (RESOURCE.LAYOUT.CH0804_THEMEDEMO4); }    }}

Because no theme is specified in the file, it automatically applies the custom theme that is specified in the manifest file for all pages.

"Android" 8.4 lets the theme adapt to different Android versions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.