如何使用现有自定义主题隐藏 XML 中活动的标题栏
我想隐藏某些活动的标题栏。问题是我将样式应用于所有活动,因此我不能简单地将主题设置为 @android:style/Theme.NoTitleBar
。
使用 NoTitleBar 主题作为我的样式的父主题将从我的所有活动中删除标题栏。
我可以在某处设置无标题样式项目吗?
I want to hide the titlebar for some of my activities. The problem is that I applied a style to all my activities, therefore I can't simply set the theme to @android:style/Theme.NoTitleBar
.
Using the NoTitleBar theme as a parent for my style would remove the title bar from all of my activities.
Can I set a no title style item somewhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
在您的
onCreate()
方法中执行此操作。this
指的是Activity
。Do this in your
onCreate()
method.this
refers to theActivity
.您可以修改
AndroidManifest.xml
:如果您不需要全屏 Activity,则可以使用
android:theme="@android:style/Theme.Black.NoTitleBar"
。注意:如果您之前使用过“默认”视图,您可能还应该将父类从
AppCompatActivity
更改为Activity
。You can modify your
AndroidManifest.xml
:or use
android:theme="@android:style/Theme.Black.NoTitleBar"
if you don't need a fullscreen Activity.Note: If you've used a 'default' view before, you probably should also change the parent class from
AppCompatActivity
toActivity
.我现在做了以下事情。
我声明了一个继承了我的通用样式的所有内容的样式,然后禁用了 titleBar。
现在,我可以将此样式设置为每个要隐藏标题栏的活动,覆盖应用程序范围样式并继承所有其他样式信息,因此样式代码中不会重复。
要将样式应用到特定的 Activity,请打开
AndroidManifest.xml
并将以下属性添加到activity
标记;I now did the following.
I declared a style inheriting everything from my general style and then disabling the titleBar.
Now I can set this style to every Activity in which I want to hide the title bar overwriting the application wide style and inheriting all the other style informations, therefor no duplication in the style code.
To apply the style to a particular Activity, open
AndroidManifest.xml
and add the following attribute to theactivity
tag;我不喜欢
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
因为标题栏会短暂出现,然后消失。我也不喜欢
android:theme="@android:style/Theme.NoTitleBar"
因为我丢失了新设备用户已经习惯的所有 3.0+ Holo 更改。所以我遇到了这个解决方案。在您的 res/values 文件夹中创建一个名为 styles.xml 的文件(如果该文件尚不存在)。在该文件中放置以下代码:
接下来使用另一个 styles.xml 文件创建一个 res/values-v11 (这可能已经存在)。在该文件中放置以下代码:
如果您的目标是 4.0+,请使用另一个 styles.xml 文件创建一个 res/values-v14 文件夹(是的,它可能已经在那里)。在该文件中放置以下代码:
最后,创建所有这些文件后,打开 AndroidManifyingst.xml 文件,您可以将代码添加到:
到您不希望为其添加标题的活动的活动标记或如果您希望它应用于整个应用程序,请使用应用程序标记。
现在,您的用户将获得与其设备版本相关的主题以及您想要的屏幕布局。
PS 将值更改为
android:theme="@style/Theme.FullScreen"
将具有相同的效果,但也会删除通知栏。I don't like the
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
because the title bar appears briefly, then disappears.I also don't like the
android:theme="@android:style/Theme.NoTitleBar"
because I lost all of the 3.0+ Holo changes that the users of the new devices have gotten used to. So I came across this solution.In your res/values folder make a file called styles.xml (If it doesn't already exist). In that file place the following code:
Next create a res/values-v11 with another styles.xml file (Once again this may already exist). In that file place the following code:
And if you are targeting 4.0+, create a res/values-v14 folder with yet another styles.xml file (Yes it may already be there). In that file place the following code:
Finally, with all of these files created, open your AndroidManifiest.xml file you can add the code:
to the activity tag of the activity you want no title for or the application tag if you want it to apply to the entire application.
Now your users will get the themes associated with their device version with the screen layout you desire.
P.S. Changing the value to
android:theme="@style/Theme.FullScreen"
will have the same effect, but also remove Notification bar.可以通过 Android 开发者页面上提到的两种方式删除标题栏:
在
manifest.xml
文件中:如果需要,请在
application
中添加以下内容为应用程序中的所有活动删除它:或针对特定活动:
The title bar can be removed in two ways as mentioned on the developer Android page:
In the
manifest.xml
file:Add the following in
application
if you want to remove it for all the activities in an app:Or for a particular activity:
正确的答案可能是不扩展 ActionbarActivity 而只是扩展 Activity(
如果您仍然使用 Actionbar Activity)
似乎这有效:
似乎这也有效:
我可以像斯科特·比格斯所写的那样。这种作品。除非那时没有主题。我的意思是设置菜单的背景是透明的:
只需更改
为 Activity 或 FragmentActivity
但是我可以使用材料设计使其看起来足够好而不是删除
操作栏:
https://gist.github.com/shimondoodkin/86e56b3351b704a05e53
这是材料设计兼容性操作栏样式的示例。
the correct answer probably is to not extend ActionbarActivity rather extend just Activity
if you still use actionbar activity
seems this is working:
seems this works too:
i could do like as Scott Biggs wrote. this kind of works. except there is no theme then. i mean the settings menu's background is transparent:
just change
to Activity or FragmentActivity
however i could make it look good enough using material design and not remove
the actionbar:
https://gist.github.com/shimondoodkin/86e56b3351b704a05e53
it is by example of material design compatibility actionbar styling.
什么对我有用:
1- 在 styles.xml 中:
2- 在 MainActivity
清单中继承样式:
what works for me:
1- in styles.xml:
2- in MainActivity
in manifest inherit the style:
如果您使用
this.requestWindowFeature(Window.FEATURE_NO_TITLE)
,当 Activity 通过onCreate
启动时,用户仍然可以在启动动画期间暂时看到标题栏。如果您使用@android:style/Theme.Black.NoTitleBar
如下所示,则在启动动画期间不会显示标题栏。上面的示例显然会覆盖您现有的应用程序主题,如果您有现有主题,请添加
- true
到其中。If you use
this.requestWindowFeature(Window.FEATURE_NO_TITLE)
user will still be able to see the title bar just for a moment during launch animation when activity starts throughonCreate
. If you use@android:style/Theme.Black.NoTitleBar
as shown below then title bar won't be shown during launch animation.above example will obviously override your existing application theme, if you have existing theme then add
<item name="android:windowNoTitle">true</item>
to it.当我尝试使用所有这些高票答案时,我的应用程序总是崩溃。
(我认为这与“@android:style”有关?)
对我来说最好的解决方案是使用以下内容:
不再有标题/标题栏。只需将其放在
...
或...
中,具体取决于您是否(不这样做) t) 希望它出现在整个应用程序中或只是一个特定的活动中。when i tried to use all those high upvoted answers my app always crashed.
(i think it has something to do with the "@android:style"?)
The best solution for me was to use the following:
No header / title bar anymore. Just place it in the
<application>...</application>
or<activity>...</activity>
depending if you (don't) want it in the whole app or just a specific activity.创建一个主题如下。
Create a theme as below.
添加
在 AppTheme (styles.xml) 内
Add
<item name="android:windowNoTitle">true</item>
inside AppTheme (styles.xml)
只需在主活动
onCreate()
方法中使用getActionBar().hide();
即可。Just use
getActionBar().hide();
in your main activityonCreate()
method.对于
AppCompat
,以下解决方案对我有用:在
styles.xml
中添加没有操作栏的新主题样式,并设置parent="Theme.AppCompat.NoActionBar"
。现在在
androidManifest.xml
中为您的启动屏幕活动实现相同的主题样式,结果如下:
For
AppCompat
, following solution worked for me:Add new theme style with no action bar in your
styles.xml
and setparent="Theme.AppCompat.NoActionBar"
.Now implement the same theme style to your splash screen activity in
androidManifest.xml
Here is result:
则只需更改 Style.xml 中的
AppTheme
样式定义从
DarkActionBar
替换为NoActionBar
,如果将AndroidManifast.xml 中定义的 AppTheme
You just need to change
AppTheme
style in Style.xml if you replace the definition fromDarkActionBar
to
NoActionBar
the AppTheme defined in AndroidManifast.xml
我相信在 2020 中对此只有一行答案
将以下行添加到 styles.xml
I believe there is just one line answer for this in 2020
Add the following line to the styles.xml
在您的
onCreate
方法中,使用以下代码片段:In your
onCreate
method, use the following snippet:您可以在 java 文件中使用此代码,
然后在设置或加载视图之前添加此行
You can use this code in your java file
add this line before you set or load your view
为您使用的主题添加这两个:
Add both of those for the theme you use:
将此样式添加到您的 style.xml 文件中,
然后在您不希望看到标题栏的特定活动中将此样式名称引用到您的 androidManifest.xml 中,如下所示。
Add this style to your style.xml file
After that reference this style name into your androidManifest.xml in perticular activity in which you don't want to see titlebar, as like below.
我正在使用支持小部件工具栏 v7。
因此,为了能够删除或隐藏标题,我们需要这样写。
I'm using a support widget Toolbar v7.
So, in order to be able to delete or hide the Title we need to write this.
为了隐藏标题和操作栏,我这样做了:
在Manifest的活动标签中:
在setContentView之前的Activity.java中:
即,
//删除标题栏
//去掉通知栏
注意:您需要在
setContentView
之前保留这些行To Hide both Title and Action bar I did this:
In activity tag of Manifest:
In Activity.java before setContentView:
i.e.,
//Remove title bar
//Remove notification bar
NOTE: You need to keep these lines before
setContentView
我想要:-
主题样式如下: -
还在 onCreate 方法中的 super.onCreate(savedInstanceState) 之后放置以下代码
I would like to prefer:-
Theme style like:-
Also put below code after super.onCreate(savedInstanceState) in onCreate menthod
或者,如果您想随时隐藏/显示标题栏:
Or if you want to hide/show the title bar at any point:
就我而言,如果您使用的是 android studio 2.1,并且您的编译 SDK 版本是 6.0,那么只需转到您的 manifest.xml 文件,并更改以下代码:
这是代码:
这是截图(请参阅突出显示代码):
In my case, if you are using android studio 2.1, and your compile SDK version is 6.0, then just go to your manifest.xml file, and change the following code:
Here is the code:
And here is the snip shoot(see the highlight code):
这解决了我的问题
在清单中我的活动:
在“AppTheme”名称下的样式中:
This Solved my problem
In manifest my activity:
In style under "AppTheme" name:
第一个答案是角闪石。
这是我的解释:
添加:
在 oncreate() 方法中。
之前:(
不仅仅是在 setContentView 之前)
如果不这样做,你将被强制关闭。
+1 这个答案。
First answer is amphibole.
here is my explain:
add:
in oncreate() method.
before:
(not just before setContentView)
if don't do this u will get forceclose.
+1 this answer.
我发现可能出现此错误的两个原因。
一。 窗口标志已在
super.onCreate(savedInstanceState);
中设置,在这种情况下,您可能需要使用以下命令顺序:二。 TitleBar 内有“后退/向上”按钮,这意味着当前活动是另一个活动的分层子级,在这种情况下,您可能需要注释掉或从
onCreate 内部删除这行代码方法。
I found two reasons why this error might occur.
One. The Window flags are set already set inside
super.onCreate(savedInstanceState);
in which case you may want to use the following order of commands:Two. You have the Back/Up button inside the TitleBar, meaning that the current activity is a hierarchical child of another activity, in which case you might want to comment out or remove this line of code from inside the
onCreate
method.如果您按照用户 YaW 和 Doug Paul 的说法进行操作,那么您必须记住,必须在调用 setContentView 之前设置窗口功能。如果没有,您将得到一个例外。
If you do what users YaW and Doug Paul say, then you have to have in mind that window features must be set prior to calling setContentView. If not, you will get an exception.
这就是完整代码的样子。注意 android.view.Window 的导入。
This is how the complete code looks like. Note the import of android.view.Window.
在 AndroidManifest.xml 上的 Activity 中添加主题 @style/Theme.AppCompat.Light.NoActionBar,如下所示
Add theme @style/Theme.AppCompat.Light.NoActionBar in your activity on AndroidManifest.xml like this