- Android Application Development Cookbook(Second Edition)
- Rick Boyer Kyle Mew
- 488字
- 2021-07-09 19:36:23
Turning a style into a theme
A theme is a style applied to an Activity or the whole application. To set a theme, use the android:theme
attribute in the AndroidManifest.xml
file. The theme
attribute applies to the <Application>
element as well as the <Activity>
elements. All views within that element will be styled with the theme specified.
It's common to set the Application theme, but then override a specific Activity with a different theme.
In the previous recipe, we set the textViewStyle
using the AppTheme style (which the wizard created automatically.) In this recipe, you will learn how to set both the Application and Activity themes.
Along with the style settings we have already explored, there are additional style options we didn't discuss because they don't apply to a View, they apply to the window as a whole. Settings such as hiding the application title or Action Bar and setting the window background, just to name a few, apply to the window and therefore must be set as a theme.
For this recipe, we are going to create a new theme based on the auto-generated AppTheme
. Our new theme will modify the window appearance to make it a dialog. We will also look at the theme
settings in the AndroidManifest.xml
.
Getting ready
Start a new project in Android Studio and call it Themes
. Use the default wizard options and select the Empty Activity when prompted for the Activity type.
How to do it...
We start by adding a new theme to the existing styles.xml
file to make our activity look like a dialog. Here are the steps to create the new theme and set activity to use the new theme:
- Since themes are defined in the same resource as styles, open the
styles.xml
file located inres/values
and create a new style. We will create a new style based on the AppTheme already provided, and setwindowIsFloating
. The XML will be as follows:<style name="AppTheme.MyDialog"> <item name="android:windowIsFloating">true</item> </style>
- Next, set the Activity to use this new dialog theme. Open the
AndroidManifest.xml
file and add atheme
attribute to the Activity element, as shown:<activity android:name=".MainActivity" android:theme="@style/AppTheme.MyDialog">
Note that both Application and Activity will now have a theme specified.
- Now run the application on a device or emulator to see the dialog theme in action.
How it works...
Our new theme MyDialog
inherits the base AppTheme
using the alternative parent declaration, since AppTheme
is defined in our code (and not a system theme). As mentioned in the introduction, some settings apply to the window as a whole, which is what we see with the windowIsFloating
setting. Once our new theme is declared, we assign our theme to the activity in the AndroidManifest
file.
There's more...
You might have noticed we could have just added the windowIsFloating
to the existing AppTheme
and been done. Since this application only has one Activity, the end result would be the same, but then, any new activities would also appear as a dialog.
- GitLab Cookbook
- 自己動手實現(xiàn)Lua:虛擬機、編譯器和標準庫
- Mastering Articulate Storyline
- 匯編語言程序設計(第3版)
- MongoDB權威指南(第3版)
- Linux Device Drivers Development
- PhoneGap:Beginner's Guide(Third Edition)
- Learning Raspbian
- QGIS Python Programming Cookbook(Second Edition)
- 深入理解C指針
- 深入理解BootLoader
- 一步一步跟我學Scratch3.0案例
- WildFly Cookbook
- Go語言編程之旅:一起用Go做項目
- 代碼整潔之道:程序員的職業(yè)素養(yǎng)