Android设置锚点到底部面板

3
我想在我的底部面板上设置一个锚点:
<!-- bottom sheets -->
<LinearLayout
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical"
    android:padding="10dp"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bottom sheets test! " />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! bottom sheets content! " />
</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"
    app:layout_anchor="@id/bottom_sheet"
    app:layout_anchorGravity="bottom|end" />

我将FAB作为CoordinateLayout的直接子项添加,并将底部表单的ID设置为FAB的layout_anchor,然后设置它的layout_anchorGravity。但是它不起作用,FAB不会移动。


你也实现了 Java 代码吗? - sasikumar
@sasikumar 我的底部表单工作正常,但是锚点不起作用。锚点需要一些Java代码吗? - Allen Vork
3个回答

3

谢谢,它可以工作。但是我发现当我拖动锚点时,底部表格不会移动。当我拖动底部表格时,它可以正常工作。我错过了什么吗? - Allen Vork
@AllenVork 看起来不是,它的工作正常。这里只有底部表格可以拖动,锚点意味着该视图将跟随其锚定的另一个视图。 - Anton Shkurenko

0

试试这个:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"
    app:layout_anchor="@id/bottom_sheet"
    app:layout_anchorGravity="end" />

它不起作用,只是改变了FAB的位置。 - Allen Vork

0

这对我有效。只需确保它在协调布局内。

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_fab"
        app:layout_anchor="@id/bottom_sheet"
        app:layout_anchorGravity="top|end" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接