Android 去掉titlebar的最優解決方案

今日在做項目時出現瞭gralloc out of memory的錯誤,經過幾日的測試調整,最終確定是在AndroidManifest.xml中使用瞭android:theme="@android:style/Theme.Translucent.NoTitleBar" 的配置項導致的,所以在隱藏titlebar時,不建議使用該方法。

經過試驗,使用一下方式可以達到同樣效果,且不會出現oom的錯誤。

在onCreate方法中調用:

1
requestWindowFeature(Window.FEATURE_NO_TITLE);
然後再在style.xml中配置:

1
<style name="Theme.MyTheme" parent="android:style/Theme.Translucent">
2
    <item name="android:windowContentOverlay">@null</item>
3
</style>
 最後在AndroidManifest.xml的application配置:

1
<application android:icon="@drawable/n_icon" android:label="@string/app_name" android:theme="@style/Theme.MyTheme">
經過以上設置,可避免oom錯誤,同時實現隱藏titlebar的效果。 

摘自 wangzunren
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *