底部导航栏上方的底部工作表

7
我的目标是在BottomNavigationView上方放置一个“底部弹出菜单”,效果如下所示:

enter image description here

但是,它们的显示效果如下。两个视图都被压缩了:

enter image description here

这是我的主活动的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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="@drawable/tierrota"
tools:context="com.example.juanjose.myapplication.ViajesActivity">

<!-- include main content -->
<include layout="@layout/bottomsheet" />

<!-- include bottom sheet -->
<include layout="@layout/bottom_navigation" />

</android.support.design.widget.CoordinatorLayout>

bottom_navigation 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemBackground="@color/colorClarito"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

底部菜单的代码

<?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/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="80dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<TextView
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@color/colorBackgroundSemi"
    android:gravity="center"
    android:text="Bandeja de entrada"
    android:fontFamily="@font/eraslght"
    android:textColor="@color/colorLetra"
    app:layout_anchor="@+id/bottom_navigation"
    app:layout_anchorGravity="top"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="coisa2"
    android:textColor="@android:color/white" />

</LinearLayout>

我对这两个元素还不熟悉。有没有人知道实现我所需求的方法?

我希望我的“底部表”能够像此类组件一样进行展开,并且我的最终目标是在 BottomSheet 中添加一个 RecyclerView


你想让底部表单像底部表单一样工作吗?从你的例子来看,如果你不想对底部表单进行动画显示/隐藏,那么一个简单的“ImageView”和“TextView”就可以完成任务。 - Reaz Murshed
我希望它能够像这样运作,我已经编辑了我的问题,谢谢。 - doblejota93
请看我的答案。希望这对你有帮助。 - Reaz Murshed
6个回答

7
为了使用 BottomSheet,它应该是 CoordinatorLayout 的子元素。这里我使用相对布局作为底部导航的父布局,使其保持在底部,然后将 CoordinatorLayout 放置在底部导航上方。 这是一篇文章,可以帮助你:article
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_above="@id/bottom_navigation_parent"
        android:layout_height="match_parent">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="?attr/colorPrimary" />


        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/bottom_sheet_parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#000000"
            app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

            <LinearLayout
                android:id="@+id/bottom_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <View
                    android:layout_width="20dp"
                    android:layout_height="4dp"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/view_bottom_sheet_top" />

                <TextView
                    android:id="@+id/near_by"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/normal"
                    android:gravity="center"
                    android:includeFontPadding="false"
                    android:padding="10dp"
                    android:text="Book Now"
                    android:textAllCaps="true"
                    android:textColor="?attr/colorPrimaryText"
                    android:textSize="12sp" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view_maps"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/bottom_view"
                android:layoutAnimation="@anim/layout_animation">

            </android.support.v7.widget.RecyclerView>

        </RelativeLayout>



    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/bottom_navigation_parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:fitsSystemWindows="true"
        app:elevation="1dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorContainerBackground"

            />
<!--              Start BottomNavigationView -->

       
<!--              End BottomNavigationView -->


    </android.support.design.widget.AppBarLayout>


</RelativeLayout>

不要忘记在你的Activity/Fragment中添加这个。
private RelativeLayout bottomSheetParentLayout;
    private BottomSheetBehavior mBottomSheetBehaviour;

   mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheetParentLayout);

  if (bottomNavigation != null) {

        mBottomSheetBehaviour.setPeekHeight(bottomNavigation.getHeight() + 90);

    }

Output will look like this


这种方法假定我们没有类似CollapsingToolbarLayout/Toolbar的东西。那么这些应该如何处理? - Otieno Rowland
这将是一个不同的问题需要回答@RowlandMtetezi,更多信息请参见链接https://dev59.com/wOk6XIcBkEYKwwoYBvbx#37474350 - iamnaran

2

将这两个元素放在一个线性布局中。不确定协调器布局的行为,但我认为它不允许您“排序”元素(类似于帧布局)。


1
如果您还有CollapsingToolBar/Toolbar的排列,这是最好的解决方案:
<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:clickable="true">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/cord_main_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fitsSystemWindows="true"
        app:layout_constraintBottom_toTopOf="@id/home_bottom_navigation_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/home_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/home_frag_toolbar"
                style="@style/Widget.Toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:logo="@drawable/ic_infinet_logo_white"
                app:navigationIcon="@drawable/ic_back_arrow" />

        </com.google.android.material.appbar.AppBarLayout>

        <FrameLayout
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:id="@+id/home_frag_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <FrameLayout
            android:id="@+id/bottom_sheet_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:clickable="true"
            app:behavior_hideable="true"
            app:behavior_peekHeight="@dimen/mini_player_height"
            app:behavior_skipCollapsed="false"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

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

            <fragment
                android:id="@+id/player_mini_frag_container"
                android:name="com.kokonetworks.kokonet.player.PlayMusicMiniFragment"
                android:layout_width="match_parent"
                android:layout_height="@dimen/mini_player_height" />

        </FrameLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/home_bottom_navigation_view"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        app:itemBackground="@color/colorPrimary"
        app:itemHorizontalTranslationEnabled="false"
        app:itemIconTint="@drawable/nav_item_background"
        app:itemTextColor="@drawable/nav_item_background"
        app:labelVisibilityMode="auto"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/home_bottom_navigation_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

它确保了:
  • BottomSheet是CoordinatorLayout的子级
  • BottomNavigation仍停留在底部
  • CollapsingToolbar/AppBarLayout的布局仍按预期放置

1
根据我的理解,您希望在底部工作表中显示RecyclerView。这使得问题变得更加容易。让我告诉您如何做。
您需要为底部导航栏设置固定高度,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_alignParentBottom="true"
    app:itemBackground="@color/colorClarito"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

现在在您的底部表中,使用clipToPadding属性配置RecyclerView。这将在您的RecyclerView底部具有一些填充。其想法是在底部表展开时,不要覆盖RecyclerView的可见区域。

以下是您应该在底部表中声明RecyclerView的方法。

<android.support.v7.widget.RecyclerView
    android:id="@+id/my_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingBottom="80dp" />

请注意,我将paddingBottom设置为80dp,这恰好是导航视图的高度。

希望这可以帮到您。


0
要使用BottomSheet,它应该是CoordinatorLayout的子元素,就像iamnaran所说的那样。 所以简单来说:
1. 将所有内容放置在RelativeLayout中 2. 将BottomSheet放置在CoordinatorLayout中 3. 将BottomNavigation的属性"alignParentBottom"设置为true 4. 在/res/value中创建新的xml文件并添加以下内容:
 <resources>
   <dimen name="bottomNavigationHeight">50dp</dimen>
 </resources>
5. 将CoordinatorLayout(BottomSheet的父级)的MarginBottom设置为
android:layout_marginBottom="@dimen/bottomNavigationHeight"

您可以通过将CoordinatorLayout的MarginBottom设置为以下值来跳过第4步和第5步:

    android:layout_marginBottom="50dp"

但这可能会在不久的将来导致混淆

整个代码应该看起来像这样:

<RelativeLayout
    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"
    >

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/clBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/bottomNavigationHeight">

        <FrameLayout
            android:id="@+id/standardBottomSheet"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:elevation="9dp"
            style="?attr/bottomSheetStyle"
            app:layout_behavior = "com.google.android.material.bottomsheet.BottomSheetBehavior"
            app:behavior_peekHeight="80dp">

        </FrameLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bttm_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_menu_nav"
        >
    </com.google.android.material.bottomnavigation.BottomNavigationView>

</RelativeLayout>

我正在使用'com.google.android.material:material:1.2.0-alpha05'。 - pepig1998

0
这对我有用。
val Int.dp: Int
    get() = (this * Resources.getSystem().displayMetrics.density).toInt()

class SomeDialogFragment : BottomSheetDialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        // Translate the bottom sheet so that it appears above the Tab Bar
        val contentView = View.inflate(context, R.layout.fragment_bottom_sheet, null)
        dialog.setContentView(contentView)
        (contentView.parent as View).translationY = (-56.dp).toFloat()

        ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接