Android - Making activity full screen with status

2019-01-31 08:24发布

问题:

I want to make my activity full screen with status bar on top of it like this picture:

I have used this code in manifest inside activity tag:

'android:theme="@style/Theme.AppCompat.Light.NoActionBar"'

But my view doesn't start from the status bar & it looks like this:

How can I make my activity look like the first one?

回答1:

I know that the guy asking the question may have found his own solution but for the people who are still looking for a solution this is a very simple solution but one thing it has a limitation till Kitkat so a condition is added

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                      WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}


回答2:

Add these to your Base Application Theme in styles.xml

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

And add following attribute to your parent layout:

android:fitsSystemWindows="true"

Hope this helps.



回答3:

If you dont want your "Translucent navigation" transparent, here is the code to just make transparent the status bar

In your theme:

    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>

In your activity onCreate:

Window window = getWindow();
WindowManager.LayoutParams winParams = window.getAttributes();
winParams.flags &= ~WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
window.setAttributes(winParams);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);


回答4:

you can use this code after onCreate

   setTheme(android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

your activity will be fullscrean with status bar :)



回答5:

Try Like This

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

Hope this one will help



回答6:

@tahsinRupam answer is correct and tested on post Kitkat version.

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

If you want to use it only for a certain activity, create a style, and attach it on your manifest style like this.

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Activity.Fullscreen.Theme" />

<style name="Activity.Fullscreen.Theme" parent="MyMaterialTheme.Base">  
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>

Thumbs up on origal @tahsinRupam

Happy codings Cheers !



回答7:

Put this code in your Activity onCreate this will hide 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();