【问题标题】:How to hide toolbar in android?如何在android中隐藏工具栏?
【发布时间】:2022-01-10 12:20:58
【问题描述】:

我真的是 android 新手,正在使用其中一个 google 模板从事一个小项目;带有标签的标签式活动...有没有办法隐藏标题部分(tabbedExample)并只保留标签?

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>

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

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

Manifyt.xml

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

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

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

</manifest>

【问题讨论】:

    标签: android android-studio android-layout


    【解决方案1】:

    您可以简单地将工具栏隐藏在您的活动中,并将其放入 oncreate 方法中:

    getSupportActionBar().hide();
    

    【讨论】:

    • 奇怪的是actionBar.hide() 不起作用,即使它是一种方法
    • 那是因为actionBar和supportActionBar不一样,它们是Android SDK内部的两种不同的实现。旧版本是 actionBar,除了旧 Android 版本的所有其他兼容性实现之外,支持库还添加了 supportActionBar。
    【解决方案2】:

    对于活动:

    getSupportActionBar().hide();
    

    对于 Java 中的片段:

    ((MainActivity) requireActivity()).getSupportActionBar().hide();
    

    对于 kotlin 中的 Fragment:

    (requireActivity() as MainActivity).supportActionBar!!.hide()
    

    【讨论】:

      【解决方案3】:

      而不是这个:

      <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>
      
      <style name="AppTheme.NoActionBar">
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
      </style>
      
      <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
      
      <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
      
      </resources>
      

      这样写:

      <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>
          <item name="windowActionBar">false</item>
          <item name="windowNoTitle">true</item>
      </style>
      
      <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
      
      <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
      
      </resources>
      

      【讨论】:

        【解决方案4】:

        在你的styles.xml文件中设置这个主题:

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

        【讨论】:

          【解决方案5】:

          您应该在 onCreate() 中尝试this.requestWindowFeature(Window.FEATURE_NO_TITLE);

          或者更简单,将您的主题设置为:@android:style/Theme.NoTitleBar

          回答了非常相似的问题here

          你应该在那里找到更多答案。

          【讨论】:

            【解决方案6】:

            在创建方法上使用这个:

            getActionBar().hide();
            

            【讨论】:

              【解决方案7】:

              这对我有帮助

              supportActionBar?.hide();
              

              【讨论】:

              • 请用edit 解释您的答案与其他答案有何不同。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-07-12
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多