Category: C #, Android, VS2015;
Date Created: 2016-02-17 First, create this chapter sample main interface
1. Interface
2, the corresponding code in the MainActivity.cs file
Add the following code in the Createchitems () method:
Chitems.add (NewChapter () {Chaptername="8th Chapter Styles and Themes", Chapteritems=Newchitem[] {NewChitem {type =typeof(CH0801THEMEDEMO1), Title="Example 8-1 Basic usage of the topic", Desc="demonstrates how to set application-level themes and topics that are only used on this page" }, NewChitem {type =typeof(Ch0802themedemo2), Title="example 8-2 dynamic selection and setting of themes", Desc="demonstrates how to dynamically select and set themes that are used only by the current interface" }, NewChitem {type =typeof(CH0803THEMEDEMO3), Title="example 8-3 Custom Theme", Desc="demonstrates how to customize a theme and its style to make it more consistent with your ideas" }, NewChitem {type =typeof(Ch0804themedemo4), Title="example 8-4 let the theme adapt to different Android versions", Desc="demonstrates how to make different Android systems automatically use the appropriate theme" }, } });
Ii. basic usage of the theme
1. View the theme
In the design interface of the. axml file, you can directly observe the effect of a theme app. The method is as follows: in the corresponding design interface, click "Theme" à "more Themes", this will pop up all the topics shown:
Select a theme, a stand-alone "OK" button that will apply the selected theme to the current design interface.
Note: This approach is just for the purpose of observing which theme is appropriate. Once you have identified the appropriate topic, you also need to specify the selected topic in the manifest file or in the active file, and it will really work.
2. Specify the subject
There are two ways to specify topics, including built-in themes and custom themes.
(1) Specify the theme used by default for all pages in the manifest file
You can choose one of the following two forms, but don't specify them in either form.
The first is to configure the "application level" theme in the Androidmanifest.xml file under the Properties folder, and the application-level theme is valid for all pages. For example:
<application android:label= "MYAPP"
Android:theme= "@android: Style/theme.devicedefault.light" >
</application>
The second approach is to configure the application-level theme in the AssemblyInfo.cs file under the Properties folder, and the application-level theme is valid for all pages. For example:
[Assembly:application (Theme = "@android: Style/theme.devicedefault.light")]
(2) Specify the theme used on this page in activity
In addition to specifying application-level topics, you can specify topics that apply only to the currently active page in an activity (. cs file), and the theme that you specify in this way is only valid for that activity, and other activities will still use the default theme. For example:
[Activity (Theme = "@android: Style/theme. DeviceDefault.Light.DarkActionBar ",
Label = "MyApp", Mainlauncher = true, Icon = "@drawable/icon")] third, material theme (Material Theme)
Material (Material) also called material, learned 3D development of the concept of Material this is certainly not unfamiliar, no matter how Chinese translation it, remember this is the English word OK.
Material theme is a built-in theme that starts with Android 5.0 (API 21) and is commonly used:
- theme.material– Black material background, this is the default theme for Android 5.0.
- theme.material.light– light material background.
- theme.material.light.darkactionbar– a light material background, but the active bar (ActionBar) is a black material background.
This topic provides a lot of flexibility because the material can be set to various types of graphics or images.
Requirements for using material theme
(1) Android 5.0 (API 21) or a later version of the Android SDK must be installed.
(2) The Android application needs to be compiled using Java JDK 1.7 or later JDK.
(3) The xamarin.android version cannot be less than 4.2.
Because the installation development environment described in the previous section has met these requirements, you can use the material theme directly.
Note: The xamarin.android version of Xamarin for VS 4.0.1717 is 6.0.0.35 and you can view the xamarin.android version of your current installation in VS2015 with help.
The following code shows how to use the material theme at the application level (valid for all activity):
<application android:label= "MYAPP"
Android:theme= "@android: Style/theme.material.light" >
</application>
You can also configure it in the AssemblyInfo.cs file. For example:
[Assembly:application (theme= "@android: Style/theme.material.light")]
The following code shows how to use the material theme in activity (only valid for that activity):
[Activity (Label = "MyApp", Mainlauncher = true, Icon = "@drawable/icon",
Theme = "@android: Style/theme.material.light")]
public class Mainactivity:activity
{
......
} Iv. Example-ch0801themedemo
The example is only intended to observe the effects of specifying the application-level theme and only applicable to an active theme.
1. Operation
2. Code Analysis
As can be seen from the run, because the left image does not specify theme in the Ch0801ThemeDemo1.cs file, it uses a theme that is defined in the manifest file (androidmanifest.xml), and the theme specified in this manner applies to all pages.
The relevant code in the manifest file (androidmanifest.xml) is as follows:
<android:label= "Mydemos" android:theme= "@android: style/ Theme.DeviceDefault.Light.DarkActionBar "> ... </ Application >
The code for the Ch0801ThemeDemo1.cs file is as follows:
usingAndroid.app;usingAndroid.os;usingAndroid.widget;namespacemydemos.srcdemos{[Activity (Label="basic usage of "Example 8-1" topic")] Public classch0801themedemo1:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (RESOURCE.LAYOUT.CH0801_THEMEDEMO1); Findviewbyid<Button> (RESOURCE.ID.BTN1). Click + =Delegate{startactivity (typeof(Ch0801themedemo2)); }; } }}
For the right graph in the run, because the theme is specified in the Ch0801ThemeDemo2.cs file, this page uses the theme instead of the theme specified in the manifest file, but the theme applies only to this page.
The code for the Ch0801ThemeDemo2.cs file is as follows:
usingAndroid.app;usingAndroid.os;usingAndroid.widget;namespacemydemos.srcdemos{[Activity (Label="basic usage of "Example 8-1" topic", Theme="@android: Style/theme.devicedefault")] Public classch0801themedemo2:activity {protected Override voidOnCreate (Bundle savedinstancestate) {Base. OnCreate (savedinstancestate); Setcontentview (RESOURCE.LAYOUT.CH0801_THEMEDEMO2); Findviewbyid<Button> (RESOURCE.ID.BTN1). Click + =Delegate{startactivity (typeof(CH0801THEMEDEMO1)); }; } }}
Basic usage of "Android" 8.1 theme