继承Acticity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉标题栏,但是还是会显示系统的状态栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
注意:requestWindowFeature(Window.FEATURE_NO_TITLE)要放在 setContentView(R.layout.activity_main)之前。
如果想要标题栏加系统状态栏一起去掉,进行如下操作:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉标题栏,但是还是会显示系统的状态栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//去掉系统状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
}
}
继承AppCompatActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//仅去掉标题栏,系统状态栏还是会显示
if (getSupportActionBar()!=null){
getSupportActionBar().hide();
}
//去掉系统状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
补充:修改xml文件实现无标题 转载:https://blog.youkuaiyun.com/qq_28585471/article/details/75991613
方法1-1:
1、查看清单文件AndroidManifest.xml中的theme
android:theme="@style/AppTheme"(系统默认的) 保持不变
2、在style.xml文件中修改AppTheme
方法1-2
在清单文件AndroidManifest.xml中修改theme,使用系统自带的无标题样式
实现无标题栏(但有系统自带的任务栏)
android:theme = "@android:style/Theme.NoTitleBar
实现全屏效果:
android:theme = "@android:style/Theme.NoTitleBar.Fullscreen"
!!!这时,可能会有朋友发现自己运行后出现错误,提示You need to use a Theme.AppCompat theme (or descendant) with this activity.这是因为Activity继承自了android.support.v7.app.AppCompatActivity,而不是android.app.Activity。具体的解决方法有两种:
1)如果不是强烈要求我们的Activity必须继承自AppCompatActivity,就直接让它继承Activity.
2)如果还是想继承自AppCompatActivity,那么根据提示来使用AppCompat的theme,即将AndroidManifest.xml文件中关于Activity的theme配置改为:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
附:Android系统自带样式
android:theme = “@android:style/Theme.Dialog” 将一个Activity显示为对话框模式
android:theme = “@android:style/Theme.NoTitleBar” 不显示应用程序标题栏
android:theme = “@android:style/Theme.NoTitleBar.Fullscreen” 不显示应用程序标题栏,并全屏
android:theme = “Theme.Light” 背景为白色
andorid:theme = “Theme.Light.NoTitleBar” 白色背景并无标题栏
android:theme = “Theme.Black” 背景为黑色
android:theme = “Theme.Black.NoTitleBar” 黑色背景并无标题栏
android:theme = “Theme.Black.NoTitleBar.Fullscreen” 黑色背景,无标题栏,全屏
android:theme = “Theme.Wallpaper” 用系统桌面为应用程序背景
android:theme = “Theme.Wallpaper.NoTitleBar” 用系统桌面为应用程序背景,且无标题栏
android:theme = “Theme.Wallpaper.NoTitleBar.Fullscreen” 用系统桌面为应用程序背景,无标题栏,全屏
android:theme = “Translucent” 透明背景
android:theme = “Theme.Translucent.NoTitleBar” 透明背景并无标题
android:theme = “Theme.Translucent.NoTitleBar.Fullscreen” 透明背景并无标题,全屏
android:theme = “Theme.Panel” 面板风格显示
android:theme = “Theme.Light.Panel” 平板风格显示