android float 操作按钮动态更改 View anchor

标签 android material-design android-design-library floating-action-button

我在 fragment 的布局文件中有一个 float 操作按钮,它表示添加数据的操作。我使用了设计支持库的协调布局,并在 CoordinationLayout 内放置了包含 2 个 View 的线性布局和一个 float 操作按钮。

 <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:id="@+id/dataLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

            <View
                android:id="@+id/viewA"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.6"
                android:background="@android:color/holo_purple"
             />

            <View
                android:id="@+id/viewB"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.4"

                android:background="@android:color/holo_orange_light"
            />

        </LinearLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/actionAddMembers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add_white_24dp"
            app:borderWidth="0dp"
            app:elevation="4dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_gravity="bottom|right"/>
    </android.support.design.widget.CoordinatorLayout>

现在,最初包含 2 个 View 的线性布局将具有 visibility=gone。当我单击 float 操作按钮时,我想将 View 置于前面。当 View 出现在屏幕上时,我想将 FAB 锚定到 View A 的末尾。我已经看到我可以将 anchor 放入布局文件中,这使得我的 View 静态。我想在运行时更改此设置,当单击 FAB 时出现 View A 时。我在 FLoatingActionButton 对象中找不到任何相关方法来动态更改 anchor 。 请建议我如何完成这项任务

最佳答案

您可以分配anchorId,检索CoordinatorLayout.LayoutParams

您可以使用这样的代码:

CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
p.setAnchorId(xxxx);
fab.setLayoutParams(p);

关于android float 操作按钮动态更改 View anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408450/

相关文章:

android - TextInputLayout 在用户关注它之前不显示 EditText 提示

android - 如何在 "TextInputLayout"的右端动态添加一个 drawable?

Android Layer-List VectorDrawable大小

c# - Material Design WPF,如何获取颜色以在边框上使用它

android - 如何使用 Material 设计来挑选时间?

Android NavigationView 菜单组分隔符

安卓 : Having trouble using visibility on xml layout file

android - 在 Android 中创建自定义 ROM

android - CollapsingToolbar - Activity 标题覆盖选项卡

android - 无法在 TextInputLayout 内的 EditText 中使用可绘制对象