【问题标题】:coordinator layout anchor doesnt work协调器布局锚不起作用
【发布时间】:2019-01-31 17:48:52
【问题描述】:

我无法让锚点工作。它不跟随滚动到顶部的应用栏。

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:background="@color/testGray">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"

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


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

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

    <FrameLayout
        android:id="@+id/frame_info"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="140dp"
        android:elevation="11dp"
        android:layout_gravity="center_horizontal"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="center|bottom"
        >

    </FrameLayout>



    <team.semicolon.amizeh.CustomViews.SongsListView
        android:id="@+id/songs_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@id/frame_info"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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


</android.support.constraint.ConstraintLayout>

另外,我无法让 minHeight 工作,它一直滚动到顶部。 我希望框架布局跟随应用栏并锚定它,但应用栏滚动但框架布局不滚动。

【问题讨论】:

  • 这可能是因为您使用ConstraintLayout 作为根布局而不是CoordinatorLayout。因此,将CoordinatorLayout 作为根并在顶层然后检查。
  • 我试过了,还是不行:(
  • SongsListView 现在不能用于锚定?
  • 当我滚动时,frame_info 没有跟随 appbar,这是应该的。当我将SongsListView 滚动到顶部时,appbar 也会移动到顶部,但 frame_info 保持原位

标签: android android-xml android-coordinatorlayout


【解决方案1】:

当我滚动时,frame_info 没有跟随 appbar,因为它是 应该做的。当我将 SongsListView 滚动到顶部时,应用栏 也移动到顶部,但 frame_info 保持原位

首先,我不建议这样的layout。就像我说的,将CoordinatorLayout 作为布局的根。

另外,如果您想在滚动时实现FrameLayout 跟随AppbarLayout,则需要将其添加到NestedScrollView 中,然后设置NestedScrollView

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

关注AppbarLayout

检查我关于在哪里使用上述代码的回答:appbar_scrolling_view_behaviorhttps://stackoverflow.com/a/35181870/4409113

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2017-04-18
    • 2017-07-29
    • 2016-08-09
    • 2018-06-29
    相关资源
    最近更新 更多