我想为我的一些活动隐藏标题栏。问题是,我应用了一个风格,我所有的活动,因此我不能简单地设置主题@android:style/ theme . notitlebar。

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

我可以在某个地方设置无标题样式的项目吗?


当前回答

正确的答案可能是不扩展ActionbarActivity,而只是扩展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  {

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

设置应用程序图标 设置操作栏的颜色以匹配设计。 将图标设置为设置菜单 添加更多图标(顶部按钮)

它是通过实例的材料设计兼容动作栏样式。

其他回答

或者如果你想隐藏/显示标题栏在任何点:

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

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

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

创建一个如下所示的主题。

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

我使用的是支持小部件Toolbar v7。 因此,为了能够删除或隐藏标题,我们需要这样写。

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

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

将此样式添加到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>