【问题标题】:Floating Action Button + Coordinator Layot + Position and placing issue浮动操作按钮 + 协调器 Layot + 位置和放置问题
【发布时间】:2018-07-13 07:22:35
【问题描述】:

协调器布局内的浮动操作按钮未放置在右|下。甚至无法解决。可能有关于 layout_anchor 的问题,但不知道是什么问题。

这里有代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mDrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/background_light"
                app:layout_scrollFlags="enterAlways|scroll" />

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

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="This is sample Text" />
        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            app:layout_anchor="@id/coordinator"
            app:layout_anchorGravity="bottom|right|end"
            app:layout_behavior="@string/fab_onscroll_behavior" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorAccent"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/nav_menu" />


</android.support.v4.widget.DrawerLayout>

谁能帮我解决这个问题?右下角不显示是什么原因?

【问题讨论】:

  • 您应该使用 NestedScrollview 作为锚点,而不是 coordinaotr 本身。 app:layout_anchor="@id/id_of_the_nested_scrollview"
  • 你能把你预期的输出分享为图片吗
  • @Airhant Jain 还有什么属性是 "app:layout_behavior="@string/fab_onscroll_behavior" ?
  • 我只想当用户滚动屏幕时,FAB 按钮应该向下。
  • 我不认为这种行为是被定义的。尝试删除它。

标签: android android-coordinatorlayout floating-action-button


【解决方案1】:

你的FloatingActionButton应该是这样的:

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:layout_gravity="bottom|end" />

您无需使用anchor 将其放在CoordinatorLayout 中。 AnchorAnchorGravity 仅用于将视图锚定到另一个同级视图。

【讨论】:

    【解决方案2】:

    只需像下面这样在 RelativeLayout 中添加 FAB 按钮可能会有所帮助

        <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mDrawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/background_light"
                    app:layout_scrollFlags="enterAlways|scroll" />
    
            </android.support.design.widget.AppBarLayout>
    
            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="This is sample Text" />
            </android.support.v4.widget.NestedScrollView>
    
           // try this
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <android.support.design.widget.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"/>
        </RelativeLayout>
    
        </android.support.design.widget.CoordinatorLayout>
    
        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/colorAccent"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_menu" />
    
    
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-10
      • 2016-07-26
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      相关资源
      最近更新 更多