在我的片段中,单击按钮时,我需要显示一个底部工作表对话框,但它必须从底部导航栏后面出现。这里已经解决了一个类似的问题,但是由于一些压力,我只需要通过扩展 BottomSheetDialogFragment 来创建底部工作表。
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/navigationHost"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="61dp"
android:layout_marginStart="14dp"
android:layout_marginEnd="14dp"
android:layout_marginBottom="30dp"
android:background="@drawable/bg_bottom_nav_rounded"
app:itemIconTint="@color/selector_bottom_navigation_text"
app:itemTextColor="@color/selector_bottom_navigation_text"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/navigationHost"
app:menu="@menu/dashboard_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段主页.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">
<Button
android:id="@+id/openBottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段主页.kt
class FragmentHome : Fragment(R.layout.fragment_home) {
private val binding by viewBinding(FragmentHomeBinding::bind)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.openBottomSheet.setOnClickListener {
val bottomSheetDialog: BottomSheetDialog = BottomSheetDialog()
bottomSheetDialog.show(requireActivity().supportFragmentManager, "TEST")
bottomSheetDialog.enterTransition
}
}
}
BottomSheetDialog.kt
public class BottomSheetDialog extends BottomSheetDialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.bottomsheet_login, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
更新:
截图