gpt4 book ai didi

android - 在 CoordinatorLayout 中排列一个 BottomNavigationView、一个 FAB 和一个 FrameLayout 并使其与 Snackbar 一起使用

转载 作者:行者123 更新时间:2023-12-04 09:17:12 25 4
gpt4 key购买 nike

我想放置一个 FrameLayout用于托管 Fragments , BottomNavigationView和一个 FAB里面CoordinatorLayout .我想显示一个 SnackbarBottomNavigationViewFAB上下移动以容纳Snackbar .我想出了以下布局,但我无法获得 Snackbar和 FAB 行为正确。

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".landingpage.MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
android:id="@+id/fragment_cont"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_main_activity_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/extended_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="72dp"
android:contentDescription="@string/cont_desc"
android:text="@string/chat"
app:icon="@drawable/ic_baseline_chat_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
如果我像这样设置 LayoutParams post然后该边距 72dp 边距也显示为在布局中向上抛出 FAB。如果我不使用边距,FAB 将堆叠在 BottomNavigationView 上。 .我可以用 addCallback以编程方式设置 FAB 边距。然后它也会跳跃,然后将自己对齐到正确的位置。总的来说,在用户体验方面,这不是一个好的体验。

最佳答案

I would like to display a Snackbar over the BottomNavigationViewand FAB to move up and down to accommodate the Snackbar. I have comeup with the following layout but I am unable to get Snackbar and FABbehavior correctly.


你的行为不好是因为你试图实现一些不常见的东西,或者, 没有以这种方式实现 通过使用 BottomNavigationViewFAB一起。实际上更常用的是 FAB & BottomAppBar底部的布局。

Then also it will jump and then align itself to the right position.Overall it is not a good experience UX wise.


虽然从 UX 的角度来看,这对用户来说不是一个好的体验,但是,以下是我可能建议的方法:
最佳方法:使用另一个 CoordinatorLayoutLinearLayout (支持 FAB 动画):
enter image description here
代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="Test" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
android:id="@+id/fragment_cont"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</androidx.core.widget.NestedScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/SnackBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/extended_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:text="Chat"
android:textColor="@color/white"
app:icon="@drawable/ic_baseline_send_24"
app:iconTint="@color/white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_insetEdge="bottom"
app:menu="@menu/escrow_menu" />
</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
在 Kotlin 方面:
extended_fab.setOnClickListener {

val snack: Snackbar = Snackbar.make(SnackBar, " Successfully ...!", Snackbar.LENGTH_SHORT)
snack.show()
}

2. BottomAppBar & FAB & setAnchorView() 没有 FAB 的方法动画
  • anchor 定到 FAB

  • enter image description here
    代码:
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <androidx.core.widget.NestedScrollView
    android:id="@+id/content_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <!-- your FrameLayout maybe -->

    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:text="SHOW SnakeBar"
    app:layout_anchor="@+id/content_layout"
    app:layout_anchorGravity="center" />


    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:title="Test" />
    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:focusable="true"
    app:layout_anchor="@id/bar" />

    <com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_gravity="bottom"
    android:backgroundTint="@color/colorPrimaryDark">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:drawableTop="@drawable/ic_baseline_send_24"
    android:gravity="center"
    android:orientation="vertical"
    android:text="Personal"
    android:textColor="#FFFFFF">

    </TextView>

    <TextView
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:drawableTop="@drawable/ic_baseline_send_24"
    android:gravity="center"
    android:orientation="vertical"
    android:text="Personal"
    android:textColor="#FFFFFF">

    </TextView>

    <TextView
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:drawableTop="@drawable/ic_baseline_send_24"
    android:gravity="center"
    android:orientation="vertical"
    android:textColor="#FFFFFF"
    android:visibility="invisible">

    </TextView>

    <TextView
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:drawableTop="@drawable/ic_baseline_send_24"
    android:gravity="center"
    android:orientation="vertical"
    android:text="Personal"
    android:textColor="#FFFFFF">

    </TextView>

    <TextView
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="?android:attr/selectableItemBackground"
    android:drawableTop="@drawable/ic_baseline_send_24"
    android:gravity="center"
    android:orientation="vertical"
    android:text="Personal"
    android:textColor="#FFFFFF">

    </TextView>

    </LinearLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    3. 使用 FAB - BottomNavigationView - ConstraintLayout没有 FAB动画
    enter image description here
    代码:
    <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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/floating_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:layout_margin="16dp"
    app:backgroundTint="@color/colorPrimary"
    app:elevation="10dp"
    app:layout_constraintBottom_toTopOf="@id/navigation"
    app:layout_constraintLeft_toRightOf="@id/navigation"
    app:layout_constraintRight_toLeftOf="@id/navigation"
    app:layout_constraintTop_toBottomOf="@id/navigation"
    app:layout_constraintTop_toTopOf="@id/navigation"
    app:layout_insetEdge="bottom" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/colorPrimaryDark"
    android:visibility="visible"
    app:itemIconTint="@color/white"
    app:itemTextColor="@color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/escrow_menu" />

    <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="290dp"
    android:layout_marginBottom="485dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    关于android - 在 CoordinatorLayout 中排列一个 BottomNavigationView、一个 FAB 和一个 FrameLayout 并使其与 Snackbar 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63166438/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com