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

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.

主站蜘蛛池模板: 涞水县| 乌海市| 玉环县| 五台县| 韶关市| 宣武区| 辉县市| 靖州| 巴彦县| 剑川县| 鸡西市| 随州市| 军事| 泰州市| 光泽县| 绵竹市| 涞源县| 内丘县| 通河县| 介休市| 武穴市| 雅江县| 长宁县| 昌平区| 吴旗县| 且末县| 建始县| 双辽市| 昔阳县| 密云县| 隆子县| 绍兴县| 辛集市| 盐津县| 龙井市| 抚顺县| 阿克| 汶川县| 尖扎县| 双牌县| 宁明县|