我想为我的一些活动隐藏标题栏。问题是,我应用了一个风格,我所有的活动,因此我不能简单地设置主题@android:style/ theme . notitlebar。
使用NoTitleBar主题作为我的样式的父主题将从我的所有活动中删除标题栏。
我可以在某个地方设置无标题样式的项目吗?
我想为我的一些活动隐藏标题栏。问题是,我应用了一个风格,我所有的活动,因此我不能简单地设置主题@android:style/ theme . notitlebar。
使用NoTitleBar主题作为我的样式的父主题将从我的所有活动中删除标题栏。
我可以在某个地方设置无标题样式的项目吗?
当前回答
咳咳,您可以将主题应用于XML中的单个活动,例如没有标题。换句话说,将其从应用程序标记中取出,打开标记声明并将其放入所需的活动标记中。
其他回答
如果你做用户YaW和Doug Paul说的,那么你必须记住在调用setContentView之前必须设置窗口特性。如果不是,您将得到一个异常。
对我有用的方法:
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:主题= >
我更喜欢:-
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)
或者如果你想隐藏/显示标题栏在任何点:
private void toggleFullscreen(boolean fullscreen)
{
WindowManager.LayoutParams attrs = getWindow().getAttributes();
if (fullscreen)
{
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
}
else
{
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
}
getWindow().setAttributes(attrs);
}
对于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>
结果如下: