- Mastering Android Studio 3
- Kyle Mew
- 879字
- 2021-07-02 18:42:33
Editor windows
Naturally, the most important of all the windows in an IDE are those where we create and modify the code that underlies all our apps. Not only do we use editors for our XML and Java, there are, among others, editors for simplifying other resources, such as translations and themes. However graphical the editor may be, all Android resources end up as XML files in the res directory.
It is quite possible to create most Android resources without ever having to write any code at all. Themes can be created with the corresponding editor with nothing more than a few clicks of a mouse. Nevertheless, if we are to consider ourselves as experts, it is important that we have a good understanding of the underlying code and how and where Studio stores these resources. The following example demonstrates how to create a new Android theme using the theme editor:
- Start or open an Android Studio project.
- Open the theme editor from Tools | Android | Theme Editor.

- From the Theme drop-down in the top right corner of the editor, select Create New Theme and enter a name in the New Theme dialog.
- Leave the Theme parent field as-is.
- Click on the colorPrimary thumbnail.
- Choose a color you like from the resultant swatch with a weight of 500.
- In the same manner, select the same color with a weight of 700 for the secondary color.
- Select a color with a weight of 100 that contrasts nicely with your primary colors for the accent.
- Open a preview or the design editor to view these changes.
In the preceding example, we created a new theme that will be automatically applied throughout the application. We could have simply edited the default AppTheme, but this approach will simplify matters if we later decide to employ more than one theme. The IDE applies these changes straightaway by adding something like the following line to the res/values/styles.xml file:
<style name="MyTheme" parent="AppTheme" />
The actual color changes can be found in the res/values/colors.xml file.
The theme editor demonstrates rather nicely how Studio editors can create and modify code after little more than a few mouse clicks from us.
It is also possible to change the theme of the IDE itself by selecting Settings | Editor | Colors and Fonts from the File menu, as displayed in the following image:

Android Studio comes equipped with just one alternative color scheme, Darcula. This theme presents light text on a dark background and, as such, is far easier on the eye than the default settings, especially for those long, late night development sessions. There are other schemes available online and it can be a lot of fun to design one's own. However, for the purposes of producing printed material, we will stick with the default IDE theme here.
Another good example of a subsidiary editor is the Translations editor, which is also a good way to demonstrate how the project structure differs from other IDEs. The following steps show how this is achieved:
- Open the Translations editor by right-clicking on the res/values/strings.xml file and selecting it from the menu. This can also be found from the Language drop-down in the design XML editor.
- Click on the globe icon near the top left corner of the editor and select a language from the list.
- Select the string you want to translate in the top pane and enter the value in the lower pane, as shown:

This is a remarkably simple exercise, the point of it being to demonstrate how Android Studio stores such resources and how it displays them. The editor has created a new strings.xml file, identical to our original in every way apart from the string values of our translated text. This file will be referred to automatically by any device that has that language set as the default by the user.
Judging by the Project Explorer, one might think that there was a project directory called strings.xml in the values directory and that it contained two strings.xml files. This, in fact, is presented this way only to help us organize our resources. An examination of the project folder on disk will show that there are in fact two (or more) folders inside the res directory named values and values-fr. Not only does this help organize our work but it also helps minimize the space our apps take up on a device, as only the resource folders that are needed are installed on an end device.

Themes and Translations are two of the least significant editors but make a good introduction to how Android Studio manages app resources. The majority of a developer's time is spent using code editors and these will, of course, be covered in depth throughout the book. However, although editors make up the core of the IDE, there are many other useful, and even vital, tools available to us, and the most commonly used of these are available from the tools margin.
- Visual Studio 2012 Cookbook
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- PyTorch自然語言處理入門與實戰
- Mastering Kotlin
- Practical Windows Forensics
- Web Application Development with MEAN
- R大數據分析實用指南
- Swift語言實戰精講
- Vue.js 2 Web Development Projects
- 響應式Web設計:HTML5和CSS3實戰(第2版)
- Regression Analysis with Python
- Emgu CV Essentials
- Mastering Elasticsearch(Second Edition)
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Natural Language Processing with Python Quick Start Guide