您的位置:首页 > 移动开发 > Android开发

android之两种设置全屏或者无标题的方法

2016-06-15 09:46 561 查看
在开发中我们经常需要把我们的应用设置为全屏或者不想要title,

这里是有两种方法的,一种是在代码中设置,另一种方法是在配置文件里改:

一、在代码中设置:

[java] view
plain copy

 print?

package jason.tutor;    

import android.app.Activity;    

import android.os.Bundle;    

import android.view.Window;    

import android.view.WindowManager;    

public class OpenGl_Lesson1 extends Activity {    

    public void onCreate(Bundle savedInstanceState) {    

        super.onCreate(savedInstanceState);    

       //无title      

       requestWindowFeature(Window.FEATURE_NO_TITLE);      

        //全屏      

       getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,        

                      WindowManager.LayoutParams. FLAG_FULLSCREEN);     

             

        setContentView(R.layout.main);    

    }    

}    

在这里要强调一点,设置全屏的俩段代码必须在setContentView(R.layout.main) 之前,不然会报错,而且这种方法有个弊端,就是在设置全屏的这个activity刚出现时,title会闪现一下再消失,也就是需要执行到设置全屏的代码的时候才会完全有效.所以为了避免出现这种情况,一般推荐用第二种方法,如下:

二、在配置文件里修改(Android:theme="@android:style/Theme.NoTitleBar.Fullscreen"):

[html] view
plain copy

 print?

<?xml version="1.0" encoding="utf-8"?>    

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    

      package="jason.tutor"    

      android:versionCode="1"    

      android:versionName="1.0">    

    <application android:icon="@drawable/icon" android:label="@string/app_name">    

        <activity android:name=".OpenGl_Lesson1"    

                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"    

                  android:label="@string/app_name">    

            <intent-filter>    

                <action android:name="android.intent.action.MAIN" />    

                <category android:name="android.intent.category.LAUNCHER" />    

            </intent-filter>    

        </activity>    

    </application>    

    <uses-sdk android:minSdkVersion="7" />    

</manifest>     

如果只是想要某一个activity全屏,那么这段代码刚刚好,如果是想整个应用的所有activity都全屏,那么如下

[html] view
plain copy

 print?

<?xml version="1.0" encoding="utf-8"?>    

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    

      package="jason.tutor"    

      android:versionCode="1"    

      android:versionName="1.0">    

    <application android:icon="@drawable/icon"   

                 android:label="@string/app_name"  

                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen">   

        <activity android:name=".OpenGl_Lesson1"    

                  android:label="@string/app_name">    

            <intent-filter>    

                <action android:name="android.intent.action.MAIN" />    

                <category android:name="android.intent.category.LAUNCHER" />    

            </intent-filter>    

        </activity>    

    </application>    

    <uses-sdk android:minSdkVersion="7" />    

</manifest>     

而当我们想使用android:theme="@android:theme="@style/AppTheme",
又想要无标题栏时,

进入android:theme="@android:style/Theme.NoTitleBar“源码下,把定义无标题的源码语句
<item name = "android:windowNoTitle">true</item>
拷贝到style/AppTheme对应源码中。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: