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

发布于 2024-08-27 22:59:29 字数 188 浏览 6 评论 0原文

我想隐藏某些活动的标题栏。问题是我将样式应用于所有活动,因此我不能简单地将主题设置为 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

时光磨忆 2024-09-03 22:59:29

在您的 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); 

this 指的是Activity

Do this in your onCreate() method.

//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); 

this refers to the Activity.

紅太極 2024-09-03 22:59:29

您可以修改 AndroidManifest.xml

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

如果您不需要全屏 Activity,则可以使用 android:theme="@android:style/Theme.Black.NoTitleBar"

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

You can modify your 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.

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

心在旅行 2024-09-03 22:59:29

我现在做了以下事情。

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

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

现在,我可以将此样式设置为每个要隐藏标题栏的活动,覆盖应用程序范围样式并继承所有其他样式信息,因此样式代码中不会重复。

要将样式应用到特定的 Activity,请打开 AndroidManifest.xml 并将以下属性添加到 activity 标记;

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

I now did the following.

I declared a style inheriting everything from my general style and then disabling the 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.xml and add the following attribute to the activity tag;

<activity
    android:theme="@style/generalnotitle">
满天都是小星星 2024-09-03 22:59:29

我不喜欢 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+,请使用另一个 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>

最后,创建所有这些文件后,打开 AndroidManifyingst.xml 文件,您可以将代码添加到:

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

到您不希望为其添加标题的活动的活动标记或如果您希望它应用于整个应用程序,请使用应用程序标记。

现在,您的用户将获得与其设备版本相关的主题以及您想要的屏幕布局。

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:

<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-v11 with another styles.xml file (Once again this may already exist). In that file place the following code:

<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-v14 folder with yet another styles.xml file (Yes it may already be there). In that file place the following code:

<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.xml file you can add the code:

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.

心是晴朗的。 2024-09-03 22:59:29

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

manifest.xml 文件中:

  1. 如果需要,请在 application 中添加以下内容为应用程序中的所有活动删除它:

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

    ;
    

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

In the manifest.xml file:

  1. Add the following in application if 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">
    
神也荒唐 2024-09-03 22:59:29

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


如果您仍然使用 Actionbar Activity)
似乎这有效:

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

似乎这也有效:

styles.xml:

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

我可以像斯科特·比格斯所写的那样。这种作品。除非那时没有主题。我的意思是设置菜单的背景是透明的:

只需更改

public class MainActivity extends ActionBarActivity {

为 Activity 或 FragmentActivity

public class MainActivity extends Activity  {

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

  1. 设置应用程序图标
  2. 设置操作颜色酒吧以匹配设计。
  3. 将图标设置为设置菜单
  4. 添加更多图标(顶部按钮),

这是材料设计兼容性操作栏样式的示例。

the correct answer probably is to not extend ActionbarActivity rather extend just 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:

just change

public class MainActivity extends ActionBarActivity {

to Activity or 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

  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)

it is by example of material design compatibility actionbar styling.

把时间冻结 2024-09-03 22:59:29

什么对我有用:

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);
}
  1. 清单中继承样式:

    ;
     清单中的

what works for me:

1- in styles.xml:

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

2- in 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">
    
甚是思念 2024-09-03 22:59:29

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

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

上面的示例显然会覆盖您现有的应用程序主题,如果您有现有主题,请添加 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 through onCreate. If you use @android:style/Theme.Black.NoTitleBar as shown below then title bar won't be shown during launch animation.

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

莫言歌 2024-09-03 22:59:29

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

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

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

不再有标题/标题栏。只需将其放在 ...... 中,具体取决于您是否(不这样做) 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:

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.

野生奥特曼 2024-09-03 22:59:29

创建一个主题如下。

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

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>
听风念你 2024-09-03 22:59:29

添加

true

在 AppTheme (styles.xml) 内

Add

<item name="android:windowNoTitle">true</item>

inside AppTheme (styles.xml)

回心转意 2024-09-03 22:59:29

只需在主活动 onCreate() 方法中使用 getActionBar().hide(); 即可。

Just use getActionBar().hide(); in your main activity onCreate() method.

傲世九天 2024-09-03 22:59:29

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

结果如下:

在此处输入图像描述

For AppCompat, following solution worked for me:

Add new theme style with no action bar in your styles.xml and set 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>

Now implement the same theme style to your splash screen activity in 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>

Here is result:

enter image description here

心碎的声音 2024-09-03 22:59:29

则只需更改 Style.xml 中的 AppTheme 样式

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

定义从 DarkActionBar 替换为 NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

如果将AndroidManifast.xml 中定义的 AppTheme

You just need to change AppTheme style in Style.xml if you replace the definition from DarkActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

to NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

the AppTheme defined in AndroidManifast.xml

酒浓于脸红 2024-09-03 22:59:29

我相信在 2020 中对此只有一行答案

将以下行添加到 styles.xml

<item name="windowNoTitle">true</item>

I believe there is just one line answer for this in 2020

Add the following line to the styles.xml

<item name="windowNoTitle">true</item>
幼儿园老大 2024-09-03 22:59:29

在您的 onCreate 方法中,使用以下代码片段:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

In your onCreate method, use the following snippet:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
无戏配角 2024-09-03 22:59:29

您可以在 java 文件中使用此代码,

然后在设置或加载视图之前添加此行

requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(R.layout.activity_main);

You can use this code in your java file

add this line before you set or load your view

requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(R.layout.activity_main);
私藏温柔 2024-09-03 22:59:29

为您使用的主题添加这两个:

    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>

Add both of those for the theme you use:

    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
今天小雨转甜 2024-09-03 22:59:29

将此样式添加到您的 style.xml 文件中,

 <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
 </style>

然后在您不希望看到标题栏的特定活动中将此样式名称引用到您的 androidManifest.xml 中,如下所示。

<activity android:name=".youractivityname"
     android:theme="@style/AppTheme.NoActionBar">
</activity>

Add this style to your style.xml file

 <style name="AppTheme.NoActionBar">
     <item name="windowActionBar">false</item>
     <item name="windowNoTitle">true</item>
 </style>

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.

<activity android:name=".youractivityname"
     android:theme="@style/AppTheme.NoActionBar">
</activity>
内心激荡 2024-09-03 22:59:29

我正在使用支持小部件工具栏 v7。
因此,为了能够删除或隐藏标题,我们需要这样写。

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

//Remove¡ing title bar
getSupportActionBar().setDisplayShowTitleEnabled(false);

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.

Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

//Remove¡ing title bar
getSupportActionBar().setDisplayShowTitleEnabled(false);
已下线请稍等 2024-09-03 22:59:29

为了隐藏标题和操作栏,我这样做了:

在Manifest的活动标签中:

android:theme="@style/AppTheme.NoActionBar"

在setContentView之前的Activity.java中:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

即,

//删除标题栏

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//去掉通知栏

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

注意:您需要在 setContentView 之前保留这些行

To Hide both Title and Action bar I did this:

In activity tag of Manifest:

android:theme="@style/AppTheme.NoActionBar"

In Activity.java before setContentView:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

i.e.,

//Remove title bar

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar

this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

NOTE: You need to keep these lines before setContentView

把梦留给海 2024-09-03 22:59:29

我想要:-

  • AppTheme(整个应用程序主题)
  • AppTheme.NoActionBar(没有操作栏或工具栏的主题)
  • 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)

I would like to prefer:-

  • AppTheme (Whole app theme)
  • AppTheme.NoActionBar (theme without action bar or toolbar)
  • AppTheme.NoActionBar.FullScreen (theme without action bar & without status bar)

Theme style like:-

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

Also put below code after super.onCreate(savedInstanceState) in onCreate menthod

super.onCreate(savedInstanceState)    
this.window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)
烟若柳尘 2024-09-03 22:59:29

或者,如果您想随时隐藏/显示标题栏:

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);
}

Or if you want to hide/show the title bar at any point:

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);
}
初吻给了烟 2024-09-03 22:59:29

就我而言,如果您使用的是 android studio 2.1,并且您的编译 SDK 版本是 6.0,那么只需转到您的 manifest.xml 文件,并更改以下代码:

这是代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesterxu.testapp2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是截图(请参阅突出显示代码):
输入图像描述这里

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:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lesterxu.testapp2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here is the snip shoot(see the highlight code):
enter image description here

匿名的好友 2024-09-03 22:59:29

这解决了我的问题

在清单中我的活动:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

在“AppTheme”名称下的样式中:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light.NoActionBar">
        **<item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>**
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

This Solved my problem

In manifest my activity:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

In style under "AppTheme" name:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme"
        parent="Theme.AppCompat.Light.NoActionBar">
        **<item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>**
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
病女 2024-09-03 22:59:29

第一个答案是角闪石。
这是我的解释:
添加:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

在 oncreate() 方法中。

之前:(

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);

不仅仅是在 setContentView 之前)
如果不这样做,你将被强制关闭。
+1 这个答案。

First answer is amphibole.
here is my explain:
add:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

in oncreate() method.

before:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);

(not just before setContentView)
if don't do this u will get forceclose.
+1 this answer.

一笑百媚生 2024-09-03 22:59:29

我发现可能出现此错误的两个原因。

一。 窗口标志已在 super.onCreate(savedInstanceState); 中设置,在这种情况下,您可能需要使用以下命令顺序:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

super.onCreate(savedInstanceState);

二。 TitleBar 内有“后退/向上”按钮,这意味着当前活动是另一个活动的分层子级,在这种情况下,您可能需要注释掉或从 onCreate 内部删除这行代码方法。

getActionBar().setDisplayHomeAsUpEnabled(true);

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:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

super.onCreate(savedInstanceState);

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.

getActionBar().setDisplayHomeAsUpEnabled(true);
猫性小仙女 2024-09-03 22:59:29

如果您按照用户 YaWDoug 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.

Oo萌小芽oO 2024-09-03 22:59:29

这就是完整代码的样子。注意 android.view.Window 的导入。

package com.hoshan.tarik.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }
}

This is how the complete code looks like. Note the import of android.view.Window.

package com.hoshan.tarik.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
    }
}
饮湿 2024-09-03 22:59:29

在 AndroidManifest.xml 上的 Activity 中添加主题 @style/Theme.AppCompat.Light.NoActionBar,如下所示

<activity
        android:name=".activities.MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    </activity>

Add theme @style/Theme.AppCompat.Light.NoActionBar in your activity on AndroidManifest.xml like this

<activity
        android:name=".activities.MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    </activity>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文