Cho*_*ski 3 android android-layout
我希望在 AppBarLayout 和布局之间锚定一个视图。我只在 Android Studio 编辑器上得到这种行为,但在真实设备上是不同的,如下所示:

我真的很困惑。我的代码是:
<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"
tools:context="view.activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="84dp"
android:layout_marginTop="@dimen/activity_vertical_margin">
<!--views on toolbar-->
</RelativeLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/activity_home_content" />
<RelativeLayout
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/circle_accent"
app:layout_anchor="@+id/whole_layout"
app:layout_anchorGravity="top|start">
<!--img view-->
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
和“ activity_home_content ”
<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:id="@+id/whole_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="view.activity.MainActivity"
tools:showIn="@layout/activity_home_toolbar">
<!--some other views-->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
刚试过。正如我在评论中提到的,你whole_layout的LinearLayout高度为 0dp。LinearLayout默认情况下,您锚定到此的任何元素都将获得 0dp 的高度(与您锚定到的视图相同)。这意味着RelativeLayout将具有 0dp 的高程。
在AppBarLayout具有4DP的高度,所以默认情况下,RelativeLayout总是会在它下面绘制。
修复
为相对布局指定一个大于或等于 4 的高度。这将解决您的问题,除了RelativeLayout现在也会投射阴影。
这是代码 RelativeLayout
<RelativeLayout
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:background="@drawable/circle_accent"
app:layout_anchor="@+id/whole_layout"
app:layout_anchorGravity="top|start"
android:elevation="4dp">
<!--img view-->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)