官术网_书友最值得收藏!

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:

  1. Since themes are defined in the same resource as styles, open the styles.xml file located in res/values and create a new style. We will create a new style based on the AppTheme already provided, and set windowIsFloating. The XML will be as follows:
    <style name="AppTheme.MyDialog">
        <item name="android:windowIsFloating">true</item>
    </style>
  2. Next, set the Activity to use this new dialog theme. Open the AndroidManifest.xml file and add a theme 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.

  3. 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.

主站蜘蛛池模板: 保定市| 宜黄县| 冕宁县| 古交市| 合山市| 永兴县| 阳曲县| 宜君县| 临武县| 绍兴市| 同仁县| 遂溪县| 双城市| 台湾省| 庆云县| 青海省| 郁南县| 平谷区| 怀宁县| 鄱阳县| 青川县| 故城县| 章丘市| 南丹县| 华坪县| 大连市| 高淳县| 遂川县| 什邡市| 永昌县| 兴城市| 浮梁县| 岗巴县| 皋兰县| 洪泽县| 克什克腾旗| 永顺县| 志丹县| 宕昌县| 天柱县| 咸宁市|