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对应源码中。
这里是有两种方法的,一种是在代码中设置,另一种方法是在配置文件里改:
一、在代码中设置:
[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对应源码中。
相关文章推荐
- Android入门--Spinner 与setDropDownViewResource自定义下拉菜单
- android保留两位小数
- Android中实现全屏、无标题栏,中途去掉标题
- NDK r9d版本下载
- Android基于讯飞语音SDK实现语音识别
- Android关于AndroidManifest.xml详细分析
- Android之Service(一):服务简介
- Android获取当前时间与星期几
- 关于转义字符
- Android 字符串截取替换函数
- Android 常用知识总结
- Android 服务消息侦听
- Android_eclispe的单词提示
- Android之Activity(一):活动简介
- Mac配置Groovy开发环境
- Android Studio打包全攻略---从入门到精通
- 电脑通过vnc控制android 手机
- Android 6.0 运行时权限检测
- 基于XMPP协议的Android即时通信系
- Android Studio开发JNI - 第一个JNI程序