无法在实现导航抽屉时隐藏操作栏

huangapple go评论57阅读模式
英文:

Unable to hide Action Bar while implementing Navigation Drawer

问题

这是 XML 代码的翻译部分:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start"
        tools:context=".ui.ui_elements.HomeActivity"
        >

<androidx.coordinatorlayout.widget.CoordinatorLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/SCREEN"
    tools:context=".ui.ui_elements.HomeActivity">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabCradleMargin="@dimen/_13sdp"
        android:paddingVertical="@dimen/_9sdp"
        app:fabCradleVerticalOffset="@dimen/_4sdp"
        style="@style/BOTTOMNAV"
        app:fabCradleRoundedCornerRadius="@dimen/_20sdp"
        app:addElevationShadow="true"
        >

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:menu="@menu/bottom_nav_menu"
            android:background="@drawable/transparent_background"
            app:itemIconSize="@dimen/_30sdp"
            android:layout_marginHorizontal="@dimen/_16sdp"
            app:labelVisibilityMode="unlabeled"
            app:itemHorizontalTranslationEnabled="false"
            />

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/addTaskFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:fabCustomSize="@dimen/_80sdp"
        app:maxImageSize="@dimen/_60sdp"
        app:srcCompat="@drawable/baseline_add_60"
        app:tint="@null"
        app:layout_anchor="@id/bottomAppbar"
        android:contentDescription="This adds creates a task"
        style="@style/FAB"
        app:backgroundTint="@null"
        tools:ignore="ContentDescription,HardcodedText" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="100"
            >
            <ImageView
                android:id="@+id/nav_menu_button_home"
                android:layout_width="@dimen/_40sdp"
                android:layout_height="@dimen/_40sdp"
                app:srcCompat="@drawable/baseline_menu_60"
                android:layout_weight="10"
                app:tint="@null"
                style="@style/NAV_MENU"
                android:clickable="true"
                android:layout_gravity="center"
                android:padding="@dimen/_5sdp"
                android:backgroundTintMode="src_in"
                android:contentDescription="@string/nav_drawer" />

            <TextView
                android:id="@+id/title"
                android:layout_weight="90"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingEnd="@dimen/_40sdp"
                android:paddingStart="@dimen/_5sdp"
                android:fontFamily="@font/font_awesome_6_free_solid_900"
                android:textStyle="bold"
                android:letterSpacing="0.1"
                android:paddingVertical="@dimen/_5sdp"
                style="@style/TITLE"
                android:text="@string/app_name"
                android:textSize="@dimen/_37sdp"
                />
        </LinearLayout>
        <FrameLayout
            android:id="@+id/fragmentFrameLoader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/_65sdp"
            />
    </LinearLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_drawer_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_drawer"
        android:backgroundTint="@color/lc3"
        app:itemTextColor="@color/lc4"
        app:itemIconTint="@color/lc1"
        app:itemIconSize="@dimen/_22sdp"
        app:subheaderTextAppearance="@color/lc1"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        />
</androidx.drawerlayout.widget.DrawerLayout>

你提到在 Activity 文件中添加了 Objects.requireNonNull(getSupportActionBar()).hide() 来隐藏 ActionBar,但仍然存在问题。你想知道如何隐藏 ActionBar。ActionBar 在实现导航抽屉功能后不再隐藏。你也明确表示想要实现导航抽屉功能,但不使用 ToolBar,并且在 API 26 上进行开发。你想知道视图放置是否存在问题。

英文:

无法在实现导航抽屉时隐藏操作栏

This is the full code of the xml.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.drawerlayout.widget.DrawerLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/drawer_layout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:fitsSystemWindows=&quot;true&quot;
tools:openDrawer=&quot;start&quot;
tools:context=&quot;.ui.ui_elements.HomeActivity&quot;
&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
style=&quot;@style/SCREEN&quot;
tools:context=&quot;.ui.ui_elements.HomeActivity&quot;&gt;
&lt;com.google.android.material.bottomappbar.BottomAppBar
android:id=&quot;@+id/bottomAppbar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;bottom&quot;
app:fabCradleMargin=&quot;@dimen/_13sdp&quot;
android:paddingVertical=&quot;@dimen/_9sdp&quot;
app:fabCradleVerticalOffset=&quot;@dimen/_4sdp&quot;
style=&quot;@style/BOTTOMNAV&quot;
app:fabCradleRoundedCornerRadius=&quot;@dimen/_20sdp&quot;
app:addElevationShadow=&quot;true&quot;
&gt;
&lt;com.google.android.material.bottomnavigation.BottomNavigationView
android:id=&quot;@+id/bottomNavigationView&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
app:menu=&quot;@menu/bottom_nav_menu&quot;
android:background=&quot;@drawable/transparent_background&quot;
app:itemIconSize=&quot;@dimen/_30sdp&quot;
android:layout_marginHorizontal=&quot;@dimen/_16sdp&quot;
app:labelVisibilityMode=&quot;unlabeled&quot; app:itemHorizontalTranslationEnabled=&quot;false&quot;
/&gt;
&lt;/com.google.android.material.bottomappbar.BottomAppBar&gt;
&lt;com.google.android.material.floatingactionbutton.FloatingActionButton
android:id=&quot;@+id/addTaskFab&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
app:fabCustomSize=&quot;@dimen/_80sdp&quot;
app:maxImageSize=&quot;@dimen/_60sdp&quot;
app:srcCompat=&quot;@drawable/baseline_add_60&quot;
app:tint=&quot;@null&quot;
app:layout_anchor=&quot;@id/bottomAppbar&quot;
android:contentDescription=&quot;This adds creates a task&quot;
style=&quot;@style/FAB&quot;
app:backgroundTint=&quot;@null&quot;
tools:ignore=&quot;ContentDescription,HardcodedText&quot; /&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;horizontal&quot;
android:weightSum=&quot;100&quot;
&gt;
&lt;ImageView
android:id=&quot;@+id/nav_menu_button_home&quot;
android:layout_width=&quot;@dimen/_40sdp&quot;
android:layout_height=&quot;@dimen/_40sdp&quot;
app:srcCompat=&quot;@drawable/baseline_menu_60&quot;
android:layout_weight=&quot;10&quot;
app:tint=&quot;@null&quot;
style=&quot;@style/NAV_MENU&quot;
android:clickable=&quot;true&quot;
android:layout_gravity=&quot;center&quot;
android:padding=&quot;@dimen/_5sdp&quot;
android:backgroundTintMode=&quot;src_in&quot;
android:contentDescription=&quot;@string/nav_drawer&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/title&quot;
android:layout_weight=&quot;90&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center&quot;
android:paddingEnd=&quot;@dimen/_40sdp&quot;
android:paddingStart=&quot;@dimen/_5sdp&quot;
android:fontFamily=&quot;@font/font_awesome_6_free_solid_900&quot;
android:textStyle=&quot;bold&quot;
android:letterSpacing=&quot;0.1&quot;
android:paddingVertical=&quot;@dimen/_5sdp&quot;
style=&quot;@style/TITLE&quot;
android:text=&quot;@string/app_name&quot;
android:textSize=&quot;@dimen/_37sdp&quot;
/&gt;
&lt;/LinearLayout&gt;
&lt;FrameLayout
android:id=&quot;@+id/fragmentFrameLoader&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_marginBottom=&quot;@dimen/_65sdp&quot;
/&gt;
&lt;/LinearLayout&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
&lt;com.google.android.material.navigation.NavigationView
android:id=&quot;@+id/nav_drawer_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_gravity=&quot;start&quot;
app:headerLayout=&quot;@layout/nav_header&quot;
app:menu=&quot;@menu/nav_drawer&quot;
android:backgroundTint=&quot;@color/lc3&quot;
app:itemTextColor=&quot;@color/lc4&quot;
app:itemIconTint=&quot;@color/lc1&quot;
app:itemIconSize=&quot;@dimen/_22sdp&quot;
app:subheaderTextAppearance=&quot;@color/lc1&quot;
android:theme=&quot;@style/Theme.AppCompat.Light.NoActionBar&quot;
/&gt;
&lt;/androidx.drawerlayout.widget.DrawerLayout&gt;

In Activity file I did add Objects.requireNonNull(getSupportActionBar()).hide()

But regardless this bug still happens, how do I hide the actionbar?

The actionbar was hidden until I implemented the Nav Drawer feature

And YES, I do want to implement Nav Drawer without ToolBar
By the way, I am doing this on API 26

Is there anything wrong in placement of views?

答案1

得分: 1

Create a custom theme that extends the base theme you are using in the app and removes the ActionBar. Add the following code to your styles.xml file:

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

Make sure you have the Theme.AppCompat family of themes set as the activity's theme in the manifest file (AndroidManifest.xml):

<activity
    android:name=".ui.ui_elements.HomeActivity"
    android:theme="@style/AppTheme.NoActionBar"
>
英文:

Create a custom theme that extends the base theme you are using in the app and removes the ActionBar. Add the following code to your styles.xml file


&lt;style name=&quot;AppTheme.NoActionBar&quot; parent=&quot;AppTheme&quot;&gt;
&lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt;
&lt;item name=&quot;windowNoTitle&quot;&gt;true&lt;/item&gt;
&lt;/style&gt;

Make sure you have the Theme.AppCompat family of themes set as the activity's theme in the manifest file (AndroidManifest.xml

&lt;activity
android:name=&quot;.ui.ui_elements.HomeActivity&quot;
android:theme=&quot;@style/AppTheme.NoActionBar&quot;
&gt;

huangapple
  • 本文由 发表于 2023年5月22日 13:40:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定