Android 如何使用现有的自定义主题隐藏 XML 中活动的标题栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2591036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:27:17  来源:igfitidea点击:

How to hide the title bar for an Activity in XML with existing custom theme

androidandroid-layouttitlebar

提问by Janusz

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.

我想隐藏我的一些活动的标题栏。问题是我对所有活动应用了一种样式,因此我不能简单地将主题设置为@android:style/Theme.NoTitleBar.

Using the NoTitleBartheme as a parent for my style would remove the title bar from all of my activities.

使用NoTitleBar主题作为我的样式的父主题会从我的所有活动中删除标题栏。

Can I set a no title style item somewhere?

我可以在某处设置无标题样式项吗?

采纳答案by Janusz

I now did the following.

我现在做了以下事情。

I declared a style inheriting everything from my general style and then disabling the titleBar.

我声明了一种样式,它继承了我的一般样式的所有内容,然后禁用了 titleBar。

<style name="generalnotitle" parent="general">
    <item name="android:windowNoTitle">true</item>
</style>

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.xmland add the following attribute to the activitytag;

要将样式应用于特定活动,请打开AndroidManifest.xml并将以下属性添加到activity标签;

<activity
    android:theme="@style/generalnotitle">

回答by YaW

Do this in your onCreate()method.

在您的onCreate()方法中执行此操作。

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here); 

thisrefers to the Activity.

this指的是Activity.

回答by Redax

You can modify your AndroidManifest.xml:

您可以修改您的AndroidManifest.xml

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

or use android:theme="@android:style/Theme.Black.NoTitleBar"if you don't need a fullscreen Activity.

或者android:theme="@android:style/Theme.Black.NoTitleBar"在不需要全屏活动时使用。

Note:If you've used a 'default' view before, you probably should also change the parent class from AppCompatActivityto Activity.

注意:如果您之前使用过“默认”视图,您可能还应该将父类从 更改AppCompatActivityActivity

回答by Devin Stewart

I don't like the this.requestWindowFeature(Window.FEATURE_NO_TITLE);because the title bar appears briefly, then disappears.

我不喜欢 ,this.requestWindowFeature(Window.FEATURE_NO_TITLE);因为标题栏短暂出现,然后消失。

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.

我也不喜欢 ,android:theme="@android:style/Theme.NoTitleBar"因为我丢失了新设备用户已经习惯的所有 3.0+ Holo 更改。所以我遇到了这个解决方案。

In your res/valuesfolder make a file called styles.xml(If it doesn't already exist). In that file place the following code:

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>

Next create a res/values-v11with another styles.xmlfile (Once again this may already exist). In that file place the following code:

接下来创建一个res/values-v11和另一个style.xml文件(这可能已经存在)。在该文件中放置以下代码:

<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>

And if you are targeting 4.0+, create a res/values-v14folder with yet another styles.xmlfile (Yes it may already be there). In that file place the following code:

如果您的目标是 4.0+,请使用另一个styles.xml文件创建res/values-v14文件夹(是的,它可能已经存在)。在该文件中放置以下代码:

<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>

Finally, with all of these files created, open your AndroidManifiest.xmlfile you can add the code:

最后,创建所有这些文件后,打开您的AndroidManifyingt.xml文件,您可以添加以下代码:

android:theme="@style/Theme.NoTitle"

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.

PS 将值更改为android:theme="@style/Theme.FullScreen"将具有相同的效果,但也会删除通知栏。

回答by whiteShadow

The title bar can be removed in two ways as mentioned on the developer Android page:

可以通过开发者 Android 页面上提到的两种方式删除标题栏:

In the manifest.xmlfile:

manifest.xml文件中:

  1. Add the following in applicationif you want to remove it for all the activities in an app:

    <application android:theme="@android:style/Theme.Black.NoTitleBar">
    
  2. Or for a particular activity:

    <activity android:theme="@android:style/Theme.Black.NoTitleBar">
    
  1. application如果要为应用程序中的所有活动删除它,请添加以下内容:

    <application android:theme="@android:style/Theme.Black.NoTitleBar">
    
  2. 或针对特定活动:

    <activity android:theme="@android:style/Theme.Black.NoTitleBar">
    

回答by Shimon Doodkin

the correct answer probably is to not extend ActionbarActivity rather extend just Activity

正确的答案可能是不扩展 ActionbarActivity 而只扩展 Activity



if you still use actionbar activity seems this is working:

如果您仍然使用操作栏活动,这似乎有效:

@Override
protected void onCreate(Bundle savedInstanceState) {          
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide(); //<< this
    setContentView(R.layout.activity_main);
}


seems this works too:

似乎这也有效:

styles.xml:

   <style name="AppBaseTheme" parent="Theme.AppCompat.Light" >
          <item name="android:windowNoTitle">true</item>   <!-- //this -->     
   </style>


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:

我可以像 Scott Biggs 写的那样做。这种作品。除了那时没有主题。我的意思是设置菜单的背景是透明的:

just change

只是改变

public class MainActivity extends ActionBarActivity {

to Activity or FragmentActivity

到 Activity 或 FragmentActivity

public class MainActivity extends Activity  {


however i could make it look good enough using material design and not remove the actionbar: https://gist.github.com/shimondoodkin/86e56b3351b704a05e53

但是我可以使用材料设计让它看起来足够好,而不是删除操作栏:https://gist.github.com/shimondoodkin/86e56b3351b704a05e53

  1. set icon of application
  2. set colors of action bar to match design.
  3. set icon to settings menu
  4. add more icons (buttons on top)
  1. 设置应用程序图标
  2. 设置操作栏的颜色以匹配设计。
  3. 将图标设置为设置菜单
  4. 添加更多图标(顶部的按钮)

it is by example of material design compatibility actionbar styling.

它以材料设计兼容性操作栏样式为例。

回答by Flowra

what works for me:

什么对我有用:

1- in styles.xml:

1-在styles.xml中:

 <style name="generalnotitle" parent="Theme.AppCompat.Light" >
          <item name="android:windowNoTitle">true</item>   <!-- //this -->     
   </style>

2- in MainActivity

2- 在 MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {          
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide(); //<< this
    setContentView(R.layout.activity_main);
}
  1. in manifest inherit the style:

    <activity android:name=".MainActivity" android:theme="@style/generalnotitle">
    
  1. 在清单中继承样式:

    <activity android:name=".MainActivity" android:theme="@style/generalnotitle">
    

回答by raj c

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 through onCreate. If you use @android:style/Theme.Black.NoTitleBaras shown below then title bar won't be shown during launch animation.

如果您使用,this.requestWindowFeature(Window.FEATURE_NO_TITLE)当活动开始时,用户仍然可以在启动动画期间看到标题栏片刻onCreate。如果您使用@android:style/Theme.Black.NoTitleBar如下所示,则在启动动画期间将不会显示标题栏。

<activity 
    android:name=".MainActivity" 
    android:label="My App"
    android:theme="@android:style/Theme.Black.NoTitleBar"
    android:screenOrientation="portrait">

above example will obviously override your existing application theme, if you have existing theme then add <item name="android:windowNoTitle">true</item>to it.

上面的例子显然会覆盖你现有的应用程序主题,如果你有现有的主题然后添加<item name="android:windowNoTitle">true</item>到它。

回答by TryToSolveItSimple

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"?)

当我尝试使用所有这些高票答案时,我的应用程序总是崩溃。(我认为这与“@android:style”有关?)

The best solution for me was to use the following:

对我来说最好的解决方案是使用以下内容:

android:theme="@style/Theme.AppCompat.NoActionBar"

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.

没有标题/标题栏了。只需将它放在<application>...</application><activity>...</activity>取决于您(不)想要它在整个应用程序中还是只是一个特定的活动。

回答by sandy

Create a theme as below.

创建一个主题如下。

 <!-- Variation on the Light theme that turns off the title -->
<style name="MyTheme" parent="android:style/Theme.Black">
    <item name="android:windowNoTitle">true</item>
</style>