Android 如何使用xml在底部导航视图上方设置片段?
我试图在底部导航视图上方显示一个片段主机。但底部导航视图占据了片段主机的一部分。我尝试了其他答案的解决方案,但仍然没有解决问题。 我使用了带有“layout_Upper”属性的相对布局和带有realted属性的约束布局来保持底部导航视图上方的片段。但是显示文本视图的片段仍然被底部导航视图切断 以下是我尝试的xml代码: 相对布局:Android 如何使用xml在底部导航视图上方设置片段?,android,android-fragments,bottomnavigationview,Android,Android Fragments,Bottomnavigationview,我试图在底部导航视图上方显示一个片段主机。但底部导航视图占据了片段主机的一部分。我尝试了其他答案的解决方案,但仍然没有解决问题。 我使用了带有“layout_Upper”属性的相对布局和带有realted属性的约束布局来保持底部导航视图上方的片段。但是显示文本视图的片段仍然被底部导航视图切断 以下是我尝试的xml代码: 相对布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
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="match_parent"
android:layout_above="@id/nav_host_fragment"
android:layout_alignParentStart="true"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
</RelativeLayout>
使用constaint布局:
<?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"
android:paddingTop="?attr/actionBarSize">
<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="match_parent"
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"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
如何使用xml在底部导航视图上方设置片段主机?尝试使用线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:paddingTop="?attr/actionBarSize">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</LinearLayout>
用0dp值替换paddingTop,这为我解决了这个问题
<?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"
android:paddingTop="0dp"
android:paddingTop="?attr/actionBarSize" <!-- Delete this line -->
android:layout_marginBottom="?attr/actionBarSize" <!-- If you have BottomNavigationView, add this line, so it doesn't overlap your list -->
>
android:layout_marginBottom=“?attr/actionBarSize”
谢谢,它很有效。但在片段顶部和标题栏底部之后仍然有一些空白。你知道为什么以及如何删除这个空白吗?@Rifat你能分享一些屏幕截图以便更好地理解吗?@SyedAhmedJamil我在Erwin Kurniawan的解决方案中添加了一张带有颜色代码的图片,以显示不需要的空白。看起来好像有一个隐藏的导航栏空间留在那里。不正确。虽然它确实解决了顶部问题,但如果您的内容非常长,就像一个列表,它会被切断,因为底部导航视图在它前面重叠。您可以添加android:layout\u marginBottom=“?attr/actionBarSize”
,我也在处理长列表:)现在看起来正确了