简体   繁体   中英

Android How to hide notification bar

I have been trying to hide the notification bar, making my app full screen:

I have tried a few sample codes but they do not work, there were:

1# android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"

2#

requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

See below for logcat errors:...

here is my code, thanks for your help:

MainActivity

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    public static final String TAG = MainActivity.class.getSimpleName();

    ActionBar actionBar;

    // Declare Tab Variable
    ActionBar.Tab Tab1, Tab2, Tab3;
    Fragment fragmentTab1 = new FragmentTab1(); 
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //Hide Action Bar
        actionBar = getSupportActionBar();              
        // Hide Actionbar Icon
        actionBar.setDisplayShowHomeEnabled(true);
        // Hide Actionbar Title
        actionBar.setDisplayShowTitleEnabled(true);
        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        Tab1 = actionBar.newTab().setText("Asian");//.setIcon(R.drawable.tab1);
        Tab2 = actionBar.newTab().setText("Euro");
        Tab3 = actionBar.newTab().setText("Black");

        // Set Tab Listeners
        Tab1.setTabListener(new TabListener(fragmentTab1));
        Tab2.setTabListener(new TabListener(fragmentTab2));
        Tab3.setTabListener(new TabListener(fragmentTab3));

        // Add tabs to actionbar
        actionBar.addTab(Tab1);
        actionBar.addTab(Tab2);
        actionBar.addTab(Tab3);

    }//-----end onCreate

//Action bar of AppCombat -------------------------------------------------------------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}//--end body

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item> 
        <item name="android:windowFullscreen">true</item>   
    </style> 
</resources>

styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <item name="android:windowNoTitle">true</item>
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="myImageView">
        <!-- 3dp so the background border to be visible -->
        <item name="android:padding">3dp</item>
        <item name="android:background">@drawable/image_border</item>
        <item name="android:scaleType">fitCenter</item>
    </style>


    <style name="WindowTitleBackground">
        <item name="android:background">@color/titlebackgroundcolor</item>
    </style>

</resources>

**LogCat Log: of sample 2# **

06-26 11:18:01.753: D/AndroidRuntime(19490): Shutting down VM
06-26 11:18:01.753: W/dalvikvm(19490): threadid=1: thread exiting with uncaught exception (group=0x411172a0)
06-26 11:18:01.753: E/AndroidRuntime(19490): FATAL EXCEPTION: main
06-26 11:18:01.753: E/AndroidRuntime(19490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brazilapps/com.example.brazilapps.MainActivity}: java.lang.NullPointerException
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.os.Looper.loop(Looper.java:137)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.ActivityThread.main(ActivityThread.java:4921)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at java.lang.reflect.Method.invoke(Method.java:511)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at dalvik.system.NativeStart.main(Native Method)
06-26 11:18:01.753: E/AndroidRuntime(19490): Caused by: java.lang.NullPointerException
06-26 11:18:01.753: E/AndroidRuntime(19490):    at com.example.brazilapps.MainActivity.onCreate(MainActivity.java:46)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.Activity.performCreate(Activity.java:5188)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-26 11:18:01.753: E/AndroidRuntime(19490):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-26 11:18:01.753: E/AndroidRuntime(19490):    ... 11 more

First of all, have you changed the layout name in the sample to your layout name? Have you tried it in this way?

requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main); // not R.layout.main

Then what is this code doing? I think It's definitely not hiding anything

//Hide Action Bar
    actionBar = getSupportActionBar();              
    // Hide Actionbar Icon
    actionBar.setDisplayShowHomeEnabled(true);
    // Hide Actionbar Title
    actionBar.setDisplayShowTitleEnabled(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM