下面的代码可以用来重绘一个片段:
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, this.getClass(), null)
.commit();
活动中的相关代码:
<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_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="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"
android:layout_height="0dp"/>
最后是片段中的相关代码:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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"
android:background="@color/background"
tools:context=".ui.list.ListFragment">
(...)
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/foreground"
app:tint="@color/white"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:srcCompat="@android:drawable/ic_input_add"
android:contentDescription="@string/add_boadskip"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
创建时FloatingActionButton
在BottomNavigationView
之上。但是当我重绘片段时,FloatingActionButton
粘在屏幕的末端,被BottomNavigationView
覆盖。我如何在重绘片段时防止这种情况发生?
1条答案
按热度按时间xu3bshqb1#
您应该使用协调器布局的力量。从fab按钮中删除重力属性并添加
app:layout_anchor=id of bottomview
:app:layout_anchorGravity=bottom|end
有关协调器布局的更多信息为here