【问题标题】:Fixed FAB button inside a scrollable RelativeLayout修复了可滚动的 RelativeLayout 内的 FAB 按钮
【发布时间】:2016-11-11 09:43:01
【问题描述】:

我有一个可滚动的 RelativeLayout,其中包含一些 EditText、ImageView 和其他 View 元素...

<ScrollView...
    <RelativeLayout
       <TextView...
       <ImageView...
       <Button...
       ...
    </RelativeLayout>
</ScrollView>

所以现在我想在其中插入一个 FAB 按钮,这样我就可以滚动所有布局(EditText、ImageView 等),但 FAB 固定在屏幕的右下角位置。我怎样才能做到这一点?提前致谢。

【问题讨论】:

    标签: android xml android-layout floating-action-button


    【解决方案1】:

    你的布局应该有这样的结构:

    <android.support.design.widget.CoordinatorLayout
     xmlns:app="http://schemas.android.com/apk/res-auto">
    
       <ScrollView
           android:id="@+id/svContent"
           .....
       </ScrollView>
    
       <android.support.design.widget.FloatingActionButton
           android:id="@+id/fab"
           .....
           app:layout_anchor="@id/svContent"
           app:layout_anchorGravity="bottom|right|end" />
    </android.support.design.widget.CoordinatorLayout>
    

    并注意属性layout_anchorlayout_anchorGravity 那里。

    【讨论】:

    • 好的,非常感谢,它可以工作,但会导致一些性能问题...“跳过 55 帧!应用程序可能在其主读取上做太多工作。”在这种情况下,我该如何处理这种情况?
    • 您可以尝试使用NestedScrollView 而不是ScrollView 并为其指定滚动行为
    【解决方案2】:

    FAB 的代码 sn-p

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="@dimen/fab_margin_bottom"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:src="@android:drawable/icon" />
    

    如果相对布局高度匹配父级,这将使 FAB 稳定在底部。

    【讨论】:

      猜你喜欢
      • 2021-06-15
      • 1970-01-01
      • 2014-12-02
      • 1970-01-01
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2018-03-28
      相关资源
      最近更新 更多