【问题标题】:remove toolbar in fragment删除片段中的工具栏
【发布时间】:2020-12-24 15:13:05
【问题描述】:

我使用了 Android 的“底部导航活动”示例,但在每个片段中,片段顶部都有一个空白工具栏(屏幕截图中屏幕顶部的白色栏)。我已经在 Manifest 文件和布局预览中应用了 NoActionBar,我看不到工具栏。如何删除每个片段顶部的工具栏?

AndroidManifest.xml:

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

<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.Quizeule">
    <activity android:name=".activities.PlayActivity" />
    <activity android:name=".activities.SplashScreenActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Themes.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Quizeule" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>
</resources>

【问题讨论】:

  • 在您的主题中添加这些 falsetrue。让我知道这项工作是否作为答案发布,
  • 您需要为特定活动的清单中的活动设置主题。
  • @ahmadbajwa 不幸的是这没有用,它没有改变任何东西
  • @rm-rf 使用该行时使用此 parent="Theme.AppCompat.Light.DarkActionBar"
  • 您能发布一个片段及其父 XML 布局吗?

标签: java android kotlin


【解决方案1】:

activity_main.xml 中删除 android:paddingTop="?actionBarSize" 即可修复错误

【讨论】:

    【解决方案2】:

    试试这个:

    如果你使用 java ;在片段 onCreate 中写入:

     GetActivity.getSupportedActionBar.hide();
    

    或者,

        // set Windows Flags to Full Screen
     getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    
        
    

    或者如果你使用 kotlin ;在 onCreateview 中写入:

    (activity as AppCompatActivity).supportActionBar!!.hide()}
    

    或者

     requiredActivity.supportActionBar!!.hide()
    

    【讨论】:

      【解决方案3】:

      您可以尝试在 Theme.Quizeule 中添加以下内容吗?

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

      【讨论】:

      • 不幸的是,这不起作用。没有任何改变
      • 提供的 2 行也已经在这个主题父项中
      【解决方案4】:

      这是Theme.MaterialComponents.DayNight.NoActionBar Material design dark Theme 的默认行为。查看this answer了解更多信息

      如果您仍想使用此 DayNight 主题,则必须删除 item name="colorPrimary" 属性并改用 item name="colorSurface"

      如果不需要深色模式效果,也可以使用Theme.MaterialComponents.NoActionBar 主题。

      【讨论】:

      • 我尝试了这两种解决方案并阅读了链接的帖子,但它只是改变了我的应用程序的颜色,但空白工具栏一直在那里
      • @rm-rf 很遗憾听到这个消息.. 它仍然与Theme.MaterialComponents.NoActionBar 一起显示吗?
      【解决方案5】:

      您必须添加这些windowActionBarwindowNoTitle

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

      【讨论】:

      • 效果不佳,我的按钮颜色没有任何变化,但它们的工具栏仍然存在
      • 尝试在特定活动中添加这个主题,像这样,
      • 效果不佳,我应该添加任何其他代码吗?会不会有其他问题?
      • 添加这个单独的主题
      猜你喜欢
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多