我想为我的一些活动隐藏标题栏。问题是,我应用了一个风格,我所有的活动,因此我不能简单地设置主题@android:style/ theme . notitlebar。
使用NoTitleBar主题作为我的样式的父主题将从我的所有活动中删除标题栏。
我可以在某个地方设置无标题样式的项目吗?
我想为我的一些活动隐藏标题栏。问题是,我应用了一个风格,我所有的活动,因此我不能简单地设置主题@android:style/ theme . notitlebar。
使用NoTitleBar主题作为我的样式的父主题将从我的所有活动中删除标题栏。
我可以在某个地方设置无标题样式的项目吗?
当前回答
对我有用的方法:
1-在styles.xml中:
<style name="generalnotitle" parent="Theme.AppCompat.Light" >
<item name="android:windowNoTitle">true</item> <!-- //this -->
</style>
2-在MainActivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); //<< this
setContentView(R.layout.activity_main);
}
在显式中继承样式: <活动android: name = "。MainActivity @style / generalnotitle“android:主题= >
其他回答
标题栏可以用两种方式删除,如开发者Android页面所述:
在manifest.xml文件中:
如果你想为应用程序中的所有活动删除它,请在应用程序中添加以下内容: <应用android:主题= " @android:风格/ Theme.Black.NoTitleBar”> 或用于某一特定活动: <活动android:主题= " @android:风格/ Theme.Black.NoTitleBar”>
对于AppCompat,以下解决方案对我来说是有效的:
在你的styles.xml中添加新的没有操作栏的主题样式,并设置parent=" theme . appcompat . noactionbar "。
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/colorPrimary</item>
</style>
现在在androidManifest.xml中为你的启动画面活动实现相同的主题样式
<activity
android:name=".ActivityName"
android:theme="@style/SplashTheme"> // apply splash them here
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
结果如下:
我更喜欢:-
AppTheme(整个应用程序主题) AppTheme。NoActionBar(没有操作栏或工具栏的主题) fullscreen(没有动作栏和状态栏的主题)
主题风格喜欢:-
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorDarkPrimary</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.NoActionBar.FullScreen" parent="AppTheme.NoActionBar">
<item name="android:windowFullscreen">true</item>
</style>
在onCreate方法中也把下面的代码放在super.onCreate(savedInstanceState)之后
super.onCreate(savedInstanceState)
this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN)
对我有用的方法:
1-在styles.xml中:
<style name="generalnotitle" parent="Theme.AppCompat.Light" >
<item name="android:windowNoTitle">true</item> <!-- //this -->
</style>
2-在MainActivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); //<< this
setContentView(R.layout.activity_main);
}
在显式中继承样式: <活动android: name = "。MainActivity @style / generalnotitle“android:主题= >
我不喜欢this.requestWindowFeature(Window.FEATURE_NO_TITLE);因为标题栏会短暂出现,然后消失。
我也不喜欢android:theme="@android:style/ theme。NoTitleBar”,因为我失去了新设备用户已经习惯的3.0+ Holo的所有变化。所以我想到了这个解。
在res/values文件夹中创建一个名为styles.xml的文件(如果它不存在的话)。在该文件中放置以下代码:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Default" parent="@android:style/Theme"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>
</resources>
接下来用另一个styles.xml文件创建res/values-v11(同样,这个文件可能已经存在)。在该文件中放置以下代码:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Default" parent="@android:style/Theme.Holo"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style>
</resources>
如果你的目标是4.0以上,创建一个res/values-v14文件夹和另一个styles.xml文件(是的,它可能已经在那里了)。在该文件中放置以下代码:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style>
<style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
<style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style>
</resources>
最后,创建好所有这些文件后,打开androidmanifest .xml文件,添加代码:
android:theme="@style/Theme.NoTitle"
如果想要应用到整个应用程序,则添加到不需要标题的活动的活动标记或应用程序标记。
现在您的用户将获得与他们的设备版本相关联的主题和您想要的屏幕布局。
附注:将值更改为android:theme="@style/ theme。“全屏”也会有同样的效果,还会删除通知栏。