如何隐藏各 Activity 中的标题栏和系统的图标栏
扫描二维码
随时随地手机看文章
1 隐藏系统的图标栏
在 setContentView(R.layout.activity_main); 之前增加:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
就可以达到隐藏系统图标栏的目的。
2 隐藏应用中各 Activity 的标题栏
在 resvalues 目录新建文件: style.xml,或在 styles.xml 中增加如下内容:
true" _ue_custom_node_="true">
然后修改 manifest 文件中 theme 的行,修改后如下:
或,使用如下语句:
android:theme="@android:style/Theme.NoTitleBar">
启动时直接出错,Log 如下:
10-15 14:18:51.282: D/AndroidRuntime(8833): Shutting down VM 10-15 14:18:51.282: W/dalvikvm(8833): threadid=1: thread exiting with uncaught exception (group=0x4001b6a8) 10-15 14:18:51.302: E/AndroidRuntime(8833): FATAL EXCEPTION: main 10-15 14:18:51.302: E/AndroidRuntime(8833): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hs.leozheng.phonelinkhs/com.hs.leozheng.phonelinkhs.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1734) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1753) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.ActivityThread.access$1500(ActivityThread.java:155) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:999) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.os.Handler.dispatchMessage(Handler.java:130) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.os.Looper.loop(SourceFile:351) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.ActivityThread.main(ActivityThread.java:3820) 10-15 14:18:51.302: E/AndroidRuntime(8833): at java.lang.reflect.Method.invokeNative(Native Method) 10-15 14:18:51.302: E/AndroidRuntime(8833): at java.lang.reflect.Method.invoke(Method.java:538) 10-15 14:18:51.302: E/AndroidRuntime(8833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969) 10-15 14:18:51.302: E/AndroidRuntime(8833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727) 10-15 14:18:51.302: E/AndroidRuntime(8833): at dalvik.system.NativeStart.main(Native Method) 10-15 14:18:51.302: E/AndroidRuntime(8833): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:110) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:99) 10-15 14:18:51.302: E/AndroidRuntime(8833): at com.hs.leozheng.phonelinkhs.MainActivity.onCreate(MainActivity.java:51) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082) 10-15 14:18:51.302: E/AndroidRuntime(8833): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1698) 10-15 14:18:51.302: E/AndroidRuntime(8833): ... 11 more
另外,尝试修改代码,在 setContentView(R.layout.activity_main); 前增加如下所示的代码:
requestWindowFeature(Window.FEATURE_NO_TITLE);
也一样没有效果。
以上两种方法(也可以说是三种)是网上提供的方法,但测试后都未达到目的。
最后得到了一种解决方法。
是 android supportv_7 包里的 Appcompat 框架,来实现 Actionbar 对吧
对于这个包里的 ActionbarActivity,他在初始化的时候,会检查你的 theme,如果主题不是 Appcompat 的主题就会报错,你改一下主题形式,让他为:
Theme.AppCompat.Light 就好了
哪里看出来用v7的?Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
如果要隐藏掉 Actionbar, 就是用:
getSupportActionBar().hide();
就好了