android 片段内容重叠图和底部导航视图

js5cn81o  于 2023-10-14  发布在  Android
关注(0)|答案(7)|浏览(142)

我在我的android应用程序中实现了一个底部导航视图。有五个碎片。然而,我有一个问题,每次我改变片段的内容重叠的工具栏。
底部导航视图的布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".DashboardActivity">
  9. <include layout="@layout/toolbar"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content">
  12. </include>
  13. <FrameLayout
  14. android:id="@+id/container"
  15. android:layout_width="match_parent"
  16. android:layout_above="@+id/navigationView"
  17. android:layout_height="match_parent"
  18. />
  19. <android.support.design.widget.BottomNavigationView
  20. android:id="@+id/navigationView"
  21. android:layout_width="0dp"
  22. android:layout_height="wrap_content"
  23. android:layout_marginEnd="0dp"
  24. android:layout_marginStart="0dp"
  25. android:background="?android:attr/windowBackground"
  26. app:layout_constraintBottom_toBottomOf="parent"
  27. app:layout_constraintLeft_toLeftOf="parent"
  28. app:layout_constraintRight_toRightOf="parent"
  29. app:itemBackground="@android:color/white"
  30. app:itemIconTint="@color/cardview_dark_background"
  31. app:itemTextColor="@android:color/black"
  32. app:menu="@menu/navigation_menu"
  33. />
  34. </android.support.constraint.ConstraintLayout>

其中一个片段布局的示例

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:id="@+id/frameLayout"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".AttendanceFragment">
  9. <android.support.constraint.Guideline
  10. android:id="@+id/guideline"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:orientation="vertical"
  14. app:layout_constraintGuide_percent="0.7" />
  15. <android.support.v7.widget.CardView
  16. android:id="@+id/cardView2"
  17. android:layout_width="0dp"
  18. android:layout_height="wrap_content"
  19. app:layout_constraintBottom_toTopOf="@+id/barrier3"
  20. app:layout_constraintEnd_toStartOf="@+id/guideline"
  21. app:layout_constraintStart_toStartOf="parent"
  22. app:layout_constraintTop_toTopOf="parent">
  23. <Spinner
  24. android:id="@+id/spinner_courses"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_marginStart="8dp"
  28. android:paddingBottom="4dp"
  29. android:paddingTop="4dp"
  30. app:layout_constraintEnd_toStartOf="@+id/guideline"
  31. app:layout_constraintStart_toStartOf="parent"
  32. app:layout_constraintTop_toTopOf="parent" />
  33. </android.support.v7.widget.CardView>
  34. <TextView
  35. android:id="@+id/tv_time"
  36. android:layout_width="0dp"
  37. android:layout_height="wrap_content"
  38. android:gravity="center"
  39. android:textColor="@color/colorPrimaryDark"
  40. android:textSize="12sp"
  41. app:layout_constraintBottom_toTopOf="@+id/barrier3"
  42. app:layout_constraintEnd_toEndOf="parent"
  43. app:layout_constraintStart_toStartOf="@+id/guideline"
  44. app:layout_constraintTop_toTopOf="parent"
  45. tools:text="Date" />
  46. <android.support.design.widget.FloatingActionButton
  47. android:id="@+id/add_fab"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:layout_gravity="bottom|right"
  51. android:layout_margin="16dp"
  52. android:src="@drawable/ic_add_icon"
  53. app:layout_constraintBottom_toBottomOf="parent"
  54. app:layout_constraintRight_toRightOf="parent"/>
  55. <!--<android.support.design.widget.FloatingActionButton
  56. android:id="@+id/floatingActionButton"
  57. android:layout_width="30dp"
  58. android:layout_height="30dp"
  59. android:layout_marginBottom="4dp"
  60. android:layout_marginEnd="8dp"
  61. android:layout_marginLeft="8dp"
  62. android:layout_marginRight="8dp"
  63. android:layout_marginStart="8dp"
  64. android:layout_marginTop="4dp"
  65. android:clickable="true"
  66. app:srcCompat="@drawable/ic_more_vert_black_24dp"
  67. app:fabSize="mini"
  68. app:layout_constraintBottom_toBottomOf="@+id/textView3"
  69. app:layout_constraintEnd_toEndOf="parent"
  70. app:layout_constraintStart_toEndOf="@+id/spinner"
  71. app:layout_constraintTop_toTopOf="parent"
  72. android:focusable="true" />-->
  73. <android.support.constraint.Barrier
  74. android:id="@+id/barrier3"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. app:barrierDirection="bottom"
  78. app:constraint_referenced_ids="cardView2,tv_time"
  79. tools:layout_editor_absoluteY="511dp" />
  80. <android.support.v7.widget.CardView
  81. android:layout_width="match_parent"
  82. android:layout_height="0dp"
  83. android:layout_marginTop="4dp"
  84. app:cardBackgroundColor="@color/colorMainBackground"
  85. app:layout_constraintBottom_toBottomOf="parent"
  86. app:layout_constraintEnd_toEndOf="parent"
  87. app:layout_constraintStart_toStartOf="parent"
  88. app:layout_constraintTop_toTopOf="@+id/barrier3">
  89. <android.support.v7.widget.RecyclerView
  90. android:id="@+id/rv_register"
  91. android:layout_width="match_parent"
  92. android:layout_height="match_parent">
  93. </android.support.v7.widget.RecyclerView>
  94. </android.support.v7.widget.CardView>
  95. </android.support.constraint.ConstraintLayout>

这是屏幕上重叠内容的样子

5q4ezhmt

5q4ezhmt1#

我从我的约束布局中删除了默认生成的marginTop字段,并将片段的layout_height更改为0dp。然后在所有Fragments的布局中,最外面的layout_height我总是设置为匹配parent。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/container"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <com.google.android.material.bottomnavigation.BottomNavigationView
  8. android:id="@+id/nav_view"
  9. android:layout_width="0dp"
  10. android:layout_height="wrap_content"
  11. android:layout_marginStart="0dp"
  12. android:layout_marginEnd="0dp"
  13. android:background="?android:attr/windowBackground"
  14. app:layout_constraintBottom_toBottomOf="parent"
  15. app:layout_constraintLeft_toLeftOf="parent"
  16. app:layout_constraintRight_toRightOf="parent"
  17. app:menu="@menu/bottom_nav_menu" />
  18. <fragment
  19. android:id="@+id/nav_host_fragment"
  20. android:name="androidx.navigation.fragment.NavHostFragment"
  21. android:layout_width="match_parent"
  22. android:layout_height="0dp"
  23. app:defaultNavHost="true"
  24. app:layout_constraintBottom_toTopOf="@id/nav_view"
  25. app:layout_constraintLeft_toLeftOf="parent"
  26. app:layout_constraintRight_toRightOf="parent"
  27. app:layout_constraintTop_toTopOf="parent" />
  28. </androidx.constraintlayout.widget.ConstraintLayout>
展开查看全部
lbsnaicq

lbsnaicq2#

向容器添加边距:

  1. <FrameLayout
  2. android:id="@+id/container"
  3. android:layout_width="match_parent"
  4. android:layout_above="@+id/navigationView"
  5. android:layout_height="match_parent"
  6. android:layout_marginBottom="{bottom_navigation_height}"
  7. android:layout_marginTop="{tool_bar_height}"
  8. />
pdkcd3nj

pdkcd3nj3#

您应该向@id/container添加约束。顶部到工具栏,底部到BottomNavigationBar。你应该对这些元素做同样的事情。顶部到父级,工具栏底部到容器。BottomNavigationBar顶部到容器,底部到父级。然后将容器的高度设置为0 dp。

  1. <include layout="@layout/toolbar"
  2. android:id="@+id/toolbar"
  3. android:layout_width="0dp"
  4. android:layout_height="?attr/actionBarSize"
  5. app:layout_constraintTop_toTopOf="parent"
  6. app:layout_constraintEnd_toEndOf="parent"
  7. app:layout_constraintStart_toStartOf="parent"/>
  8. <FrameLayout
  9. android:id="@+id/container"
  10. android:layout_width="0dp"
  11. android:layout_height="0dp"
  12. app:layout_constraintBottom_toBottomOf="@+id/navigationView"
  13. app:layout_constraintTop_toTopOf="@id/toolbar"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintStart_toStartOf="parent"/>
  16. <android.support.design.widget.BottomNavigationView
  17. android:id="@+id/navigationView"
  18. android:layout_width="0dp"
  19. android:layout_height="wrap_content"
  20. app:layout_constraintBottom_toBottomOf="parent"
  21. app:layout_constraintLeft_toLeftOf="parent"
  22. app:layout_constraintRight_toRightOf="parent"/>

没有测试代码,但应该工作。

展开查看全部
beq87vna

beq87vna4#

它的重叠,因为你没有给你的控制适当的约束。

  • 你可以试试这个方法 *
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <include
  9. android:id="@+id/include"
  10. layout="@layout/toolbar"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintTop_toTopOf="parent"/>
  15. <FrameLayout
  16. android:id="@+id/container"
  17. android:layout_width="0dp"
  18. android:layout_height="0dp"
  19. app:layout_constraintBottom_toTopOf="@+id/navigationView"
  20. app:layout_constraintEnd_toEndOf="parent"
  21. app:layout_constraintStart_toStartOf="parent"
  22. app:layout_constraintTop_toBottomOf="@+id/include" />
  23. <android.support.design.widget.BottomNavigationView
  24. android:id="@+id/navigationView"
  25. android:layout_width="0dp"
  26. android:layout_height="wrap_content"
  27. android:layout_marginEnd="0dp"
  28. android:layout_marginStart="0dp"
  29. android:background="?android:attr/windowBackground"
  30. app:layout_constraintBottom_toBottomOf="parent"
  31. app:layout_constraintLeft_toLeftOf="parent"
  32. app:layout_constraintRight_toRightOf="parent"
  33. app:itemBackground="@android:color/white"
  34. app:itemIconTint="@color/cardview_dark_background"
  35. app:itemTextColor="@android:color/black"
  36. app:menu="@menu/navigation_menu"
  37. />
  38. </android.support.constraint.ConstraintLayout>
展开查看全部
q0qdq0h2

q0qdq0h25#

在最近的Android Studio版本(4.0.beta 1)中,如果默认生成的activity_main.xml(省略属性)上的层次结构如下所示,

  1. <androidx.constraintlayout.widget.ConstraintLayout>
  2. <com.google.android.material.bottomnavigation.BottomNavigationView />
  3. <fragment />
  4. </androidx.constraintlayout.widget.ConstraintLayout>

这些片段似乎与底部导航重叠,只是重新排列,如下所示,

  1. <androidx.constraintlayout.widget.ConstraintLayout>
  2. <fragment />
  3. <com.google.android.material.bottomnavigation.BottomNavigationView />
  4. </androidx.constraintlayout.widget.ConstraintLayout>

为我修复了这个问题没有触及任何约束,也没有添加任何填充或边距

f8rj6qna

f8rj6qna6#

请检查您的MainActivity代码。如果您使用的是fragmentTransaction.add(R.id.container,fragment)
使用替换:fragmentTransaction.replace(R.id.container,fragment)
“add”添加n个片段的顶部,“replace”删除前一个并添加新的片段

hfwmuf9z

hfwmuf9z7#

你的片段容器使用了“match_parent”,它占据了工具栏和navigationBar的位置,也许你可以选择嵌套的LinearLayout,使用height=warp_content,weight=1

相关问题