底部导航上方的底部

时间:2018-02-06 18:30:21

标签: android material-design bottom-sheet bottomnavigationview

我的目标是放置一张"底页"在这样的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>

我是这两个元素的新手。是否有人知道如何实现我所寻找的目标?

我想要我的&#34;底页&#34;这样做并且可以扩展。我的最终目标是在RecyclerView内添加BottomSheet

5 个答案:

答案 0 :(得分:2)

将两个元素包裹在线性布局中。不确定协调器布局如何表现,但我认为它不会允许你按顺序排列&#34;元素(类似于框架布局)。

答案 1 :(得分: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>

现在在底部工作表中,配置RecyclerView中包含clipToPadding属性的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精确地设置为console.log(userinfo),这是导航视图的高度。

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

答案 2 :(得分:1)

要使用 BottomSheet ,它应该是 CoordinatorLayout 的子级。在这里,我使用相对布局作为底部导航的父布局,以使其停留在底部,然后在底部导航上方使用CoordinatorLayout。 希望这会有所帮助。

  <tr *ngFor="let c of myService.companyList">
    <td>{{c.name}}</td>
    <td>{{c.email}}</td>
    <td>{{c.password}}</td>
    <td> <input type="submit" value="click to remove" (click)="removeCompany(32)" (click)=/> </td>
    <td> <a href="">Click here to remove this company {{c.id}}</a></td>
  </tr>

别忘了将其添加到“活动/片段”中

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

            />

        <LinearLayout
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:layout_alignParentBottom="true"
            android:background="@android:color/transparent"
            android:baselineAligned="false"
            android:orientation="horizontal">


            <RelativeLayout
                android:id="@+id/home_parent"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="@dimen/item_padding"
                android:paddingBottom="@dimen/item_padding_bottom">

                <ImageView
                    android:id="@+id/home"
                    android:layout_width="22dp"
                    android:layout_height="22dp"
                    android:layout_centerHorizontal="true"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/ic_home_24_dp"
                    android:tint="?attr/colorAccent"

                    />

                <TextView
                    style="@style/NormalTextPrimary"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/home"
                    android:layout_marginTop="5dp"
                    android:gravity="center"
                    android:text="@string/home"
                    android:textColor="?attr/colorAccent"
                    android:textSize="12sp"
                    tools:ignore="SmallSp"

                    />
            </RelativeLayout>


            <RelativeLayout
                android:id="@+id/profile_parent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="@dimen/item_padding"
                android:paddingBottom="@dimen/item_padding_bottom">

                <ImageView
                    android:id="@+id/profile"
                    android:layout_width="22dp"
                    android:layout_height="22dp"
                    android:layout_centerHorizontal="true"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/ic_notification_24_dp"
                    android:tint="@color/colorDefault" />

                <TextView

                    style="@style/NormalTextPrimary"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/profile"
                    android:layout_marginTop="5dp"
                    android:gravity="center"
                    android:text="@string/profile"
                    android:textColor="@color/colorDefault"
                    android:textSize="10sp"
                    tools:ignore="SmallSp" />

            </RelativeLayout>


            <RelativeLayout
                android:id="@+id/notification_parent"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:focusable="true"
                android:paddingTop="@dimen/item_padding"
                android:paddingBottom="@dimen/item_padding_bottom">

                <ImageView
                    android:id="@+id/notification_icon"
                    android:layout_width="22dp"
                    android:layout_height="22dp"
                    android:layout_centerHorizontal="true"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/ic_notification_24_dp"
                    android:tint="@color/colorDefault"

                    />

                <TextView
                    style="@style/NormalTextPrimary"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/notification_icon"
                    android:layout_marginTop="5dp"
                    android:gravity="center"
                    android:text="@string/notification"
                    android:textColor="@color/colorDefault"

                    android:textSize="10sp"

                    tools:ignore="SmallSp" />
            </RelativeLayout>


        </LinearLayout>


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


</RelativeLayout>

Output will look like this

答案 3 :(得分: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布置仍如预期

答案 4 :(得分:-1)

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