【问题标题】:Cannot hide title bar无法隐藏标题栏
【发布时间】:2016-07-13 08:47:58
【问题描述】:

我是 Android 开发新手,我想在我的应用程序中隐藏标题栏。

我尽我所能隐藏该栏,但应用程序要么停止,要么没有任何改变。

我在虚拟设备上运行了演示。这是一部 5.4 英寸 FWVGA 手机。
我也尝试添加
android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen"
或
android:theme="@android:style/Theme.NoTitleBar(.Fullscreen)"
但不幸的是,应用程序停止了。

请告诉我如何隐藏标题栏。
这是我的 java 代码、布局文件、AndroidManifest.xml 和 styles.xml。

package com.activitytest2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //1.   getActionBar().hide();
    //2.   requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(FirstActivity.this,"You clicked button 1",Toast.LENGTH_SHORT).show();
        }
    });
}

}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button 1"/>
</LinearLayout>

AndroidManifest.xml

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

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

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


styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
 </resources>

【问题讨论】:

  • 您是否正在使用 AppCompat 活动,因为您需要 AppCompat 主题。使用这个 Theme.AppCompat.Light.NoActionBar

标签: java android titlebar


【解决方案1】:

走简单的路。只需替换getActionBar().hide(); 给getSupportActionBar().hide();

我希望这个解决方案对您有所帮助。

"@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();       
    setContentView(R.layout.first_layout);"

【讨论】:

  • 首先清除您的项目,然后运行 ​​Build -> Clean Project.... 它运行顺利... 我试过了。不要更改应用程序主题中的任何内容。
【解决方案2】:

不用加getActionBar().hide();

就这样吧,

“无标题”功能的标志,关闭顶部的标题 屏幕。

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

对于测试用例,将其添加到您的样式部分

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

并确保在 ma​​nifest

中
android:theme="@style/AppTheme"

仅供参考

你可以试试,

 super.onCreate(savedInstanceState);  

    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    //code that displays the content in full screen mode  
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  

【讨论】:

  • 我已经尝试了以上所有方法,但仍然没有任何改变。感谢您的帮助。
  • @AngusMurphy 你有其他解决方案吗?
  • 我接受的答案有效,但我不知道为什么。但是非常感谢您的帮助。
【解决方案3】:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

在 onCreate() 中将删除您的标题栏。

【讨论】:

    【解决方案4】:

    试一试,

     requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    在setContentView 之前。喜欢,

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

    【讨论】:

      【解决方案5】:

      您是否正在使用 AppCompat 活动,因为您需要 AppCompat 主题。 用这个 Theme.AppCompat.Light.NoActionBar

      //添加样式文件

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

      //添加清单

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

      【讨论】:

        【解决方案6】:

        将您的样式替换为以下内容:这也会隐藏您的操作栏。

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

        【讨论】:

          【解决方案7】:

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

          结果如下:

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-31
            • 2019-06-09
            • 2013-03-13
            相关资源
            最近更新 更多