Android 片段内容与工具栏和底部导航视图重叠
我在android应用程序中实现了一个底部导航视图。有五个碎片。但是,我有一个问题,每次我更改片段时,片段的内容都与工具栏重叠 底部导航视图的布局Android 片段内容与工具栏和底部导航视图重叠,android,xml,android-fragments,Android,Xml,Android Fragments,我在android应用程序中实现了一个底部导航视图。有五个碎片。但是,我有一个问题,每次我更改片段时,片段的内容都与工具栏重叠 底部导航视图的布局 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DashboardActivity">
<include layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_above="@+id/navigationView"
android:layout_height="match_parent"
/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemBackground="@android:color/white"
app:itemIconTint="@color/cardview_dark_background"
app:itemTextColor="@android:color/black"
app:menu="@menu/navigation_menu"
/>
</android.support.constraint.ConstraintLayout>
将边距添加到容器:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_above="@+id/navigationView"
android:layout_height="match_parent"
android:layout_marginBottom="{bottom_navigation_height}"
android:layout_marginTop="{tool_bar_height}"
/>
您的片段容器使用了“匹配父对象”,它占据了工具栏和导航栏的位置,也许您可以选择嵌套线性布局并使用height=warp\u content,weight=1您应该为@id/容器添加约束。
从顶部到工具栏,从底部到底部导航栏。您也应该对这些元素执行相同的操作。工具栏顶部到父级,工具栏底部到容器。底部导航栏从顶部到容器,从底部到父级。然后将容器的高度设置为0dp
<include layout="@layout/toolbar"
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<FrameLayout
android:id="@+id/container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="@+id/navigationView"
app:layout_constraintTop_toTopOf="@id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
没有测试代码,但应该可以工作。它重叠,因为您没有为控件提供适当的约束
你可以这样试试。
在最近的Android Studio版本(4.0.beta 1)中,如果默认生成的activity\u main.xml
(省略的属性)上的层次结构如下所示
<androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView />
<fragment />
</androidx.constraintlayout.widget.ConstraintLayout>
碎片似乎与底部导航重叠,只是如下图所示重新排列
<androidx.constraintlayout.widget.ConstraintLayout>
<fragment />
<com.google.android.material.bottomnavigation.BottomNavigationView />
</androidx.constraintlayout.widget.ConstraintLayout>
修复了我的问题,没有接触任何约束,也没有添加任何填充或边距,我从约束中删除了默认生成的marginTop
字段,并将片段的布局高度更改为0dp。然后在我所有片段的布局中,最外层的layou高度
我总是设置为匹配父级
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
将顶部xml中的ConstraintLayout更改为CoordinatorLayout,然后在第二个xml的ConstraintLayout中添加app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
,我将片段的布局高度更改为0dp
,效果非常好。有人能解释一下为什么它能解决这个问题吗?
<androidx.constraintlayout.widget.ConstraintLayout>
<fragment />
<com.google.android.material.bottomnavigation.BottomNavigationView />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>