标题栏系列:隐藏标题栏遇到的坑

  • 一般的写法:
//可以在setContentView()之前,也可以之后。
getActionBar().hide();

有个坑,就是会报空指针异常,原因是ActionBar是否存在于Theme有关。例如Theme设置如下,就会有空指针异常。

   //AndroidManifest.xml文件中的application标签下,设置的AppTheme
   android:theme="@style/AppTheme"

//具体设置
	<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>
    </style>

这种情况下,需要用 getSupportActionBar(),所以,快刀斩乱麻。

        if (getActionBar() != null) {
            getActionBar().hide();
        }
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

猜你喜欢

转载自blog.csdn.net/zhangjin1120/article/details/114600252