Android-嵌套CoordinatorLayout如何更改FAB的锚定行为

时间:2018-11-16 14:27:55

标签: android-coordinatorlayout

我正在尝试学习Android的CoordinatorLayout。很抱歉,在我要提出的问题之前,是否有任何问题要问,但是到目前为止,我所看到的一切对我来说都是与众不同的。无论如何。

我碰到了一篇有关CoordinatorLayout的不同用法的兴趣文章:https://www.androidauthority.com/using-coordinatorlayout-android-apps-703720/

在第二个用例(即“ FAB Follows Widget”)中,FAB的布局固定在NestedScrollView内特定CardView的右下角;全部都位于CoordinatorLayout中,而CoordinatorLayout也嵌套在RelativeLayout本身中。如果CoordinatorLayout嵌套在顶部布局视图中,则FAB从右下角开始 屏幕的可视区域。在滚动过程中,当指定的CardView的右下角进入可见区域时,FAB会粘贴在那里并跟随该CardView的右下角,直到向上滚动以离开可见的屏幕区域为止。也就是说,FAB只要在屏幕上可见,就只会停留在指定点。

使用布局进行实验,如果我们删除RelativeLayout并使用CoordinatorLayout作为根布局,则FAB在屏幕的右下角处可见。但是,当向上滚动指定的CardView时,FAB会坚持下去,并且如果进一步滚动,它就会绕过可见区域。我找不到这种行为改变的原因。

以下是第一种情况的布局XML:

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
    tools:context=".FabFollowsWidgetActivity">

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

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingTop="24dp">

                <android.support.v7.widget.CardView
                    android:id="@+id/cardView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/card_margin">

                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/first"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textSize="20sp"
                            android:text="@string/long_latin" />

                    </LinearLayout>

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

                <android.support.v7.widget.CardView
                    android:id="@+id/cardView2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/card_margin"
                    android:layout_marginLeft="@dimen/card_margin"
                    android:layout_marginRight="@dimen/card_margin">

                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/second"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textSize="20sp"
                            android:text="@string/long_latin" />

                    </LinearLayout>

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

                <android.support.v7.widget.CardView
                    android:id="@+id/cardView3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/card_margin"
                    android:layout_marginLeft="@dimen/card_margin"
                    android:layout_marginRight="@dimen/card_margin">

                    <LinearLayout
                        style="@style/Widget.CardContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/third"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textSize="20sp"
                            android:text="@string/long_latin" />

                    </LinearLayout>

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

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_horizontal_margin"
            android:src="@drawable/mascot_icon"
            app:layout_anchor="@id/cardView1"
            app:layout_anchorGravity="bottom|end" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:layout_margin="@dimen/activity_horizontal_margin"
            android:src="@drawable/mascot"
            android:layout_gravity="bottom|start"/>
    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

第二种情况是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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:fitsSystemWindows="true"
    tools:context=".FabFollowsWidgetActivity">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <android.support.v7.widget.CardView
                android:id="@+id/cardView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/card_margin">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/first"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="@string/long_latin" />

                </LinearLayout>

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

            <android.support.v7.widget.CardView
                android:id="@+id/cardView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/card_margin"
                android:layout_marginLeft="@dimen/card_margin"
                android:layout_marginRight="@dimen/card_margin">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/second"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="@string/long_latin" />

                </LinearLayout>

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

            <android.support.v7.widget.CardView
                android:id="@+id/cardView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/card_margin"
                android:layout_marginLeft="@dimen/card_margin"
                android:layout_marginRight="@dimen/card_margin">

                <LinearLayout
                    style="@style/Widget.CardContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/third"
                        android:textAppearance="@style/TextAppearance.AppCompat.Title"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="@string/long_latin" />

                </LinearLayout>

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

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/mascot_icon"
        app:layout_anchor="@id/cardView1"
        app:layout_anchorGravity="bottom|end" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/mascot"
        android:layout_gravity="bottom|start"/>
</android.support.design.widget.CoordinatorLayout>

活动代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FabFollowsWidgetActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fab_follows_widget);
    }
}

以下是Github上的项目: https://github.com/obaro/UsingCoordinatorLayout

0 个答案:

没有答案