[Android interface implementation] Overlaying the Action Bar, androidoverlaying

Source: Internet
Author: User

[Android interface implementation] Overlaying the Action Bar, androidoverlaying

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

This article is translated from NLP.

By default, the ActionBar will appear in the window of your Activity, which may reduce the size of the visible area of the remaining Activity. If you want to hide or display the ActionBar during user interaction, you can use the hide () or show () method to control whether to display the ActionBar. However, this will cause your Activity to recalculate and draw a layout based on the new area.

To avoid changing the layout when the display status of the ActionBar changes, you can set the overlay mode of the ActionBar. When we use the overlay mode, our activity can use all locations, because the system directly draws the ActionBar on the layout. This masks the top of the layout, but when the ActionBar is displayed or hidden, the system does not need to re-calculate the layout, and the layout is the same during the transition.

TIPS: If you want your layout to be partially visible behind the ActionBar, that is, a little transparent, you need to create a style for customizing an ActionBar, set a transparent background. For more information about setting a custom ActionBar background, see style the action bar.


Start Overlay Mode

To enable the Overlay mode, we need to inherit the theme of an existing ActionBar and customize the android: windowActionBarOverlay attribute to true.

Compatible with version 3.0 or later

To be compatible with this version, we can use the following code:

<resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.Holo">        <item name="android:windowActionBarOverlay">true</item>    </style></resources>

If we want to be compatible with Versions later than 2.1 and use a version-compatible library, we must customize a style that inherits from Theme. Appcompat or its subclass, just like the following code

<resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.AppCompat">        <item name="android:windowActionBarOverlay">true</item>        <!-- Support library compatibility -->        <item name="windowActionBarOverlay">true</item>    </style></resources>

Note that to be compatible with 2.1, we need to define two item attributes, one with android: and the other, corresponding to the incompatible Library and the compatible library respectively.


If we use the overlay mode, the content of the originally displayed position on the Activity may be blocked. To solve this problem, we can set the padding or margen attribute of the parent layout, to prevent content from being blocked, the following code completes this function:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="?android:attr/actionBarSize">    ...</RelativeLayout>

Used here? Android: attr/actionBarSize is a fixed value. If we want to support earlier versions, we should remove the android prefix, as shown below:

<!-- Support library compatibility --><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="?attr/actionBarSize">    ...</RelativeLayout>

In this case, both high and low versions can work normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.