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

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.

主站蜘蛛池模板: 平顺县| 曲阜市| 三明市| 怀集县| 万州区| 洛川县| 永宁县| 神农架林区| 东丽区| 宁强县| 东辽县| 永昌县| 镇雄县| 诏安县| 廊坊市| 达孜县| 靖江市| 景洪市| 建瓯市| 开远市| 探索| 灌阳县| 达拉特旗| 五大连池市| 佛学| 伊春市| 德保县| 云龙县| 灯塔市| 济宁市| 衡东县| 宁乡县| 会泽县| 承德市| 讷河市| 高密市| 临朐县| 桦甸市| 夏邑县| 公安县| 霍林郭勒市|