Android Training--Managing the System UI

Dimming the System Bars

This lesson describes how to dim the system bars (that is, the status and the navigation bars) on Android 4.0 (API level 14) and higher. Android does not provide a built-in way to dim the system bars on earlier versions.

When you use this approach, the content doesn’t resize, but the icons in the system bars visually recede. As soon as the user touches either the status bar or the navigation bar area of the screen, both bars become fully visible. The advantage of this approach is that the bars are still present but their details are obscured, thus creating an immersive experience without sacrificing easy access to the bars.

Dimming the System Bars

You can dim the status and notification bars using the SYSTEM_UI_FLAG_LOW_PROFILE flag, as follows:

// This example uses decor view, but you can use any visible view.
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_LOW_PROFILE;
decorView.setSystemUiVisibility(uiOptions);

As soon as the user touches the status or navigation bar, the flag is cleared, causing the bars to be undimmed. Once the flag has been cleared, your app needs to reset it if you want to dim the bars again.

Reveal the Status and Navigation Bars

If you want to programmatically clear flags set with setSystemUiVisibility(), you can do so as follows:

View decorView = getActivity().getWindow().getDecorView();
// Calling setSystemUiVisibility() with a value of 0 clears
// all flags.
decorView.setSystemUiVisibility(0);

Hiding the Status Bar

This lesson describes how to hide the status bar on different versions of Android. Hiding the status bar (and optionally, the navigation bar) lets the content use more of the display space, thereby providing a more immersive user experience.

Figure 1. Visible status bar.
Figure 2. Hidden status bar
Figure 2 shows an app with a hidden status bar. Note that the action bar is hidden too. You should never show the action bar without the status bar.

Hide the Status Bar on Android 4.0 and Lower

You can hide the status bar on Android 4.0 (API level 14) and lower by setting WindowManager flags. You can do this programmatically or by setting an activity theme in your app’s manifest file. Setting an activity theme in your app’s manifest file is the preferred approach if the status bar should always remain hidden in your app (though strictly speaking, you could programmatically override the theme if you wanted to). For example:

<application
    ...
    android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    ...
</application>
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // If the Android version is lower than Jellybean, use this call to hide
        // the status bar.
        if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_main);
    }
    ...
}
Hide the Status Bar on Android 4.1 and Higher

You can hide the status bar on Android 4.1 (API level 16) and higher by using setSystemUiVisibility(). setSystemUiVisibility() sets UI flags at the individual view level; these settings are aggregated to the window level. Using setSystemUiVisibility() to set UI flags gives you more granular control over the system bars than using WindowManager flags. This snippet hides the status bar:

View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();

后面还有一些内容, 感觉近期不太使用的到, 先不继续研究了, 以后再补齐吧.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值