CoordinatorLayout的layout_anchorGravity对CardView不生效该如何解决?
问题原因
- 核心原因:
CoordinatorLayout的layout_anchor、layout_anchorGravity属性只有绑定了对应Behavior的控件才能正确生效。FloatingActionButton作为Material Design组件,默认内置了适配CoordinatorLayout的锚点定位Behavior,所以位置符合预期;而CardView是通用容器组件,默认没有绑定对应Behavior,设置的锚点属性不会被解析,所以位置异常。 - 次要影响因素:你当前布局中作为锚点的
left_layout、right_layout的声明顺序在CardView和FAB之后,CoordinatorLayout测量子View时先测量悬浮控件,此时锚点控件还未完成测量,也可能导致定位偏差。
解决方案
这里提供两种可行方案,按需选择即可:
方案1(推荐):将左侧CardView替换为FloatingActionButton
你当前左侧CardView的展示效果和FAB完全一致,直接替换就能复用FAB的内置Behavior,无需额外配置,改完即可和右侧FAB对齐显示。
方案2:保留CardView,手动添加Behavior
如果必须使用CardView,给CardView添加FAB的锚点Behavior属性,同时调整布局顺序、统一锚点布局高度即可:
- 把下方水平的LinearLayout移到CardView、FAB的声明之前,保证锚点控件先完成测量
- 给CardView添加属性:
app:layout_behavior="com.google.android.material.floatingactionbutton.FloatingActionButton$Behavior" - 把
left_layout的高度从wrap_content改为match_parent,和right_layout高度保持一致,保证锚点参考区域统一
修改后的完整代码如下:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent"> <!-- 把锚点的LinearLayout移到最前面 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="2"> <LinearLayout android:id="@+id/left_layout" android:layout_width="0dp" android:layout_height="match_parent" <!-- 高度改为match_parent和右侧统一 --> android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:id="@+id/account_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:background="@color/smallFont" android:orientation="vertical"> <TextView android:id="@+id/accout_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="20dp" android:text="title" android:textColor="@color/white" android:textSize="20dp"> </TextView> <TextView android:id="@+id/account_amount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="@string/amount" android:textColor="@color/white" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:id="@+id/cat_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="50dp" android:background="@color/blue" android:orientation="vertical"> <TextView android:id="@+id/cat_title_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginTop="20dp" android:text="Title" android:textColor="@color/white" android:textSize="20sp"> </TextView> <TextView android:id="@+id/category_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:text="sub title" android:textColor="@color/white" /> </LinearLayout> </LinearLayout> </LinearLayout> <androidx.cardview.widget.CardView android:layout_width="50dp" android:layout_height="50dp" android:layout_margin="25dp" android:src="@drawable/ic_outline_done_24" app:borderWidth="0dp" app:cardBackgroundColor="@color/colorAccent" app:elevation="6dp" app:layout_anchor="@id/left_layout" app:layout_anchorGravity="top|right" <!-- 添加Behavior属性 --> app:layout_behavior="com.google.android.material.floatingactionbutton.FloatingActionButton$Behavior"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_gravity="center" android:src="@drawable/add" app:tint="@color/colorPrimary" /> </androidx.cardview.widget.CardView> <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab_category" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="25dp" android:background="@color/blue" android:fitsSystemWindows="true" android:src="@drawable/ic_outline_done_24" app:borderWidth="0dp" app:elevation="6dp" app:fabSize="normal" app:layout_anchor="@id/right_layout" app:layout_anchorGravity="top|right"> </com.google.android.material.floatingactionbutton.FloatingActionButton> </androidx.coordinatorlayout.widget.CoordinatorLayout>
当前实际效果:
预期实现效果:
内容的提问来源于stack exchange,提问作者Ali Hassan
相关产品推荐
相关产品推荐

