温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

去除titleBar

发布时间:2020-07-08 11:56:34 来源:网络 阅读:388 作者:owlger 栏目:移动开发

    新手学习之作,本文章参考http://android.blog.51cto.com/268543/636134

    本文解决activity全屏显示问题

    

去掉屏幕上的title bar有3个方法:

1. Java代码实现

@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

       setContentView(R.layout.main);
//...
}

留意这语句所在的位置的,似乎所有requestWindowFeature的操作都要放在setContentView的前面。

但使用这种方法,用户体验不太好,在Activity将要显示时,仍然会出现title bar,然后再去掉的。

2. 自定义style配置文件

在\res\values里面的style.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="android:Theme.Light">
        <!--
            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="AppTheme.NoTitleBar" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

这里应该注意,改的最好是Application theme

然后在AndroidManifest.xml文件里,给需要去掉title bar的activity的节点上加上android:theme="@style/NoTitle,代码如下:

<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/AppTheme.NoTitleBar"/>
...


3. 直接在AndroidManifest.xml中进行修改

原来我们可以无需自定义style配置的,直接调用系统的就行了:

<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@android:style/Theme.NoTitleBar"/>
...


如果我们要设置整个Application都去掉title bar,那么就设置application:

<application
android:icon="@drawable/lightbulb"android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
...

title bar还能够自定义的,请查看文章《自定义Activity标题栏(Title bar)》http://android.blog.51cto.com/268543/636134

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI