【问题标题】:Bottom Sheet Above Bottom Navigation底部导航上方的底部工作表
【发布时间】:2018-02-06 18:30:20
【问题描述】:

我的目标是在BottomNavigationView 上放置一个“底页”,如下所示:

但它保持以下方式。两个视图都崩溃了:

这是我主要活动的 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

【问题讨论】:

  • 您希望底部工作表像底部工作表一样工作吗?从您的示例来看,如果您不想动画显示/隐藏底部表格,那么简单的 ImageViewTextView 将为您完成这项工作。
  • 我希望它这样做,我已经编辑了我的问题,谢谢
  • 请看我的回答。我希望这会对你有所帮助。

标签: android material-design bottom-sheet bottomnavigationview


【解决方案1】:

要使用 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>

不要忘记将其添加到您的活动/片段中

private RelativeLayout bottomSheetParentLayout;
    private BottomSheetBehavior mBottomSheetBehaviour;

   mBottomSheetBehaviour = BottomSheetBehavior.from(bottomSheetParentLayout);

  if (bottomNavigation != null) {

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

    }

【讨论】:

  • 该方法假设我们没有 CollapsingToolbarLayout/Toolbar 之类的东西。那些要怎么处理?
  • 这将是一个不同的问题要回答@RowlandMtetezi 更多这里是一个链接stackoverflow.com/a/37474350/6560792
【解决方案2】:

将这两个元素包裹在一个线性布局中。不确定协调器布局的行为方式,但我认为它不允许您“订购”元素(类似于框架布局)。

【讨论】:

    【解决方案3】:

    据我了解您的问题,您希望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,这是导航视图的高度。

    我希望这可能会有所帮助。

    【讨论】:

      【解决方案4】:

      如果您也有 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 的排列还是按预期进行的

      【讨论】:

        【解决方案5】:

        要使用 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'
        猜你喜欢
        • 2022-09-22
        • 2019-02-20
        • 2020-12-28
        • 1970-01-01
        • 1970-01-01
        • 2020-12-11
        • 2018-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多