Android如何隐藏通知栏

时间:2014-06-26 00:52:32

标签: android android-activity android-fragments android-theme android-notification-bar

我一直试图隐藏通知栏,让我的应用全屏:

我尝试了一些示例代码,但它们不起作用,有:

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);

请参阅下面的logcat错误:...

这是我的代码,谢谢你的帮助:

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日志:样本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

1 个答案:

答案 0 :(得分:0)

首先,您是否已将示例中的布局名称更改为您的布局名称?你有没有尝试过这种方式?

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

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

那么这段代码在做什么?我认为它绝对不会隐藏任何东西

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