英文:
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.
<?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>
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
<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"
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论