CollapsingToolbarLayout与FloatingActionButton的错误问题

4
你好,我正在创建一个简单的滚动活动应用于我的安卓app中。此滚动活动有一个带视差效果的CollapsingToolbarLayout和一个固定在AppbarLayout上的浮动操作按钮。
当我向下滚动文本时,浮动操作按钮会消失,并且当我向上滚动时,该浮动操作按钮会再次出现,但是出现的位置是在页面底部,之后又会在一秒钟后移动到正确的位置(AppbarLayout)。而且在工具栏和页面内容之间有一个奇怪的边距,我无法解决它。
由于这看起来非常丑陋,所以我想知道:
1. 我如何解决这个问题? 2. 如何保持浮动操作按钮可见并锚定在已折叠的工具栏上?
以下是演示程序的缺陷:http://i.giphy.com/LPMqLSl46Yvu0.gif 以下是我的布局:
<?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="toxotes.mystikon.EventActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_event"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                app:layout_collapseMode="parallax"
                android:src="@drawable/test"
                android:scaleType="centerCrop"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_event"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_event" />

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

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

这里是content_event的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="toxotes.mystikon.EventActivity"
    tools:showIn="@layout/activity_event">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/large_text" />

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

这里是Java代码:
public class EventActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_event);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_event);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

你使用的是哪个 Android 设计支持版本? - Burhanuddin Rashid
'com.android.support:design:24.2.0' - Roberto D'Agnelli
尝试使用'com.android.support:design:24.1.1'。 - Burhanuddin Rashid
Burhanuddin,它没有起作用。 - Roberto D'Agnelli
1个回答

0

就在今天我也遇到了完全相同的问题。你需要将支持库更新到版本24.2.1。在你的构建gradle中。这对我有用。

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:cardview-v7:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'
    compile 'com.android.support:support-v13:24.2.1'
    compile 'com.android.support:design:24.2.1'
}

但是我遇到了矢量可绘制对象的问题:

java.lang.IllegalStateException: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat.

如果你遇到了同样的问题,这里有解决方案:http://android-developers.blogspot.de/2016/02/android-support-library-232.html


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