【问题标题】:floating action button overlaping bottom navigation in android浮动操作按钮与android中的底部导航重叠
【发布时间】:2018-08-24 19:05:32
【问题描述】:

我正在制作一个应用程序,其中我的浮动操作按钮与我的底部导航栏重叠 我在 SO 上进行了搜索,但没有得到任何适当的解决方案,请提供任何帮助

我已经为底部导航布局制作了一个单独的 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation"
        android:backgroundTint="@color/nav_color"
        app:itemIconTint="@color/bottom_nav_fore_color"/>
</android.support.constraint.ConstraintLayout>

这里是activity main的xml

<?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:orientation="vertical"
    android:background="@color/colorPrimary">

<com.ct.listrr.widget.BounceScrollView
    android:layout_height="match_parent"
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android" >
<LinearLayout 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:orientation="vertical"
    tools:context="com.ct.listrr.MainActivity"

    >


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="330dp"
        android:layout_height="340dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="25dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

    <LinearLayout
        android:id="@+id/club_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.ct.listrr.widget.CustomTextViewMedium
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:textSize="40sp"
            android:text="Clubs"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:textColor="@color/White"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/clubrecycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

    </LinearLayout>


    <LinearLayout
        android:id="@+id/lounge_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.ct.listrr.widget.CustomTextViewMedium
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:textSize="40sp"
            android:text="Lounge"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:textColor="@color/White"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/loungerecycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

    </LinearLayout>

    <LinearLayout
        android:id="@+id/cafe_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.ct.listrr.widget.CustomTextViewMedium
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:textSize="40sp"
            android:text="Cafe"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:textColor="@color/White"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/caferecycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

    </LinearLayout>

    <LinearLayout
        android:id="@+id/rooftop_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.ct.listrr.widget.CustomTextViewMedium
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:textSize="40sp"
            android:text="RoofTop"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:textColor="@color/White"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout_recycle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/rooftoprecycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

    </LinearLayout>
</LinearLayout>
</com.ct.listrr.widget.BounceScrollView>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_my_icon"
        android:layout_margin="16dp"
        app:rippleColor="@color/colorAccent"/>
    <include layout="@layout/bottom_nav_bar"
        android:id="@+id/bottom_nav_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

【问题讨论】:

  • 您的问题不清楚..请明确提及您需要归档什么..根据 android 设计指南,FAB 应该高于您正在创建的所有其他视图
  • 是的 fab 应该在每个视图之上,但我的 fab 没有显示在我的底部导航栏上方

标签: android xml


【解决方案1】:

在 FloatingActionButton 中使用 Layout 以上参数

android:layout_above="@id/bottom_nav_bar"

这是您的完整 xml:

    <?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:orientation="vertical"
        android:background="@color/colorPrimary">

    <com.ct.listrr.widget.BounceScrollView
        android:layout_height="match_parent"
        android:id="@+id/scrollview"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:fillViewport="true"
        xmlns:android="http://schemas.android.com/apk/res/android" >
    <LinearLayout 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:orientation="vertical"
        tools:context="com.ct.listrr.MainActivity"

        >


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="330dp"
            android:layout_height="340dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="25dp"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp" />

        <LinearLayout
            android:id="@+id/club_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <com.ct.listrr.widget.CustomTextViewMedium
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:textSize="40sp"
                android:text="Clubs"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="15dp"
                android:textColor="@color/White"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/clubrecycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

        </LinearLayout>


        <LinearLayout
            android:id="@+id/lounge_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <com.ct.listrr.widget.CustomTextViewMedium
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:textSize="40sp"
                android:text="Lounge"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="15dp"
                android:textColor="@color/White"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/loungerecycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

        </LinearLayout>

        <LinearLayout
            android:id="@+id/cafe_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <com.ct.listrr.widget.CustomTextViewMedium
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:textSize="40sp"
                android:text="Cafe"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="15dp"
                android:textColor="@color/White"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/caferecycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

        </LinearLayout>

        <LinearLayout
            android:id="@+id/rooftop_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <com.ct.listrr.widget.CustomTextViewMedium
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:textSize="40sp"
                android:text="RoofTop"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="15dp"
                android:textColor="@color/White"/>

        </LinearLayout>
        <LinearLayout
            android:id="@+id/layout_recycle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rooftoprecycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

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

        </LinearLayout>
    </LinearLayout>
    </com.ct.listrr.widget.BounceScrollView>


        <include layout="@layout/bottom_nav_bar"
            android:id="@+id/bottom_nav_bar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            />

<android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_my_icon"
            android:layout_above="@id/bottom_nav_bar"
            android:layout_margin="16dp"
            app:rippleColor="@color/colorAccent"/>

    </RelativeLayout>

【讨论】:

  • 用你的活动 main.xml 替换上面的代码,然后检查
  • 您需要从 FloatingActionButton 中移除 android:layout_alignParentBottom="true" 并需要添加 android:layout_above="@id/bottom_nav_bar"
  • 嗨,如果我删除对齐父底部,我希望工厂位于底部但在底部导航上方,那么我该如何将它安排在底部
  • 我们正在提供关系,因此您已经将父底部对齐到底部导航,因此您的浮动操作按钮将出现在底部
  • 当我尝试添加 layout_above 时,它​​在 fab 中显示未知属性
【解决方案2】:

这行得通:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    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/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_vertical_margin"
    android:text="@string/title_home"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="0dp"
    android:layout_marginStart="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation">

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_marginBottom="28dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    app:elevation="2dp"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toStartOf="parent" />

【讨论】:

    【解决方案3】:

    我实际上并没有找到任何解决方法。 我所做的是将floating action button marginBottom 设置为?attr/actionBarSize。 它看起来不会那么好,但至少它不再与屏幕重叠。

    EDIT2:我相信我找到了解决方案。尝试将父视图 marginBottom 设置为 ?attr/actionBarSize。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2020-11-19
      • 2020-03-19
      • 1970-01-01
      相关资源
      最近更新 更多