android - 如何将卡与禁用的 FAB 重叠?
问题描述
我有一个带有 aCardView
和一个 disabled的布局,ExtendedFloatingActionButton
我以编程方式设置为启用,当启用该 FAB 时一切正常,但是当我将其设置为禁用时,它在卡后面。
我试图通过为 FAB 设置更高的高度来将高度设置为卡和 FAB,但是当它被禁用时,它无论如何都会高于..
这是它的样子:
我会显示它重叠,就像启用设置为 true 时一样:
这是我的代码:
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fabInvia"
android:layout_width="wrap_content"
android:enabled="false"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:contentDescription="@string/invia"
android:text="@string/invia"
android:textColor="#fff"
app:elevation="10dp"
android:translationY="-33dp"
app:fabSize="normal"
app:icon="@drawable/ic_baseline_send"
app:iconTint="#fff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
app:elevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
...
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
该问题不是 disabled 独有的ExtendedFloatingActionButton
,任何其他视图的行为都相同。
原因:
因为sCardView
有一个特殊的高程属性app:cardElevation
,只能应用于CardView
s,所以translation-z
orapp:elevation
属性并不能解决这个问题。
解决方案:
包装ExtendedFloatingActionButton
成另一个CardView
. 但是需要一些努力才能使它CardView
看起来不存在,要做到这一点,您需要:
app:cardElevation
在两个s 上设置完全相同CardView
(这实际上解决了您遇到的海拔问题。- 将透明背景颜色设置为 new
CardView
,app:cardBackgroundColor="@android:color/transparent"
由于某种原因不起作用,但是以编程方式解决它:
cardView2.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));
还将约束/边距从外部转移ExtendedFloatingActionButton
到外部CardView
这是一个示例
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:elevation="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:cardElevation="5dp"
app:layout_constraintBottom_toBottomOf="@+id/cardView"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fabInvia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:gravity="center"
android:text="@string/invia"
android:textColor="#fff"
app:fabSize="normal"
app:iconTint="#fff" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- python - Python seaborn - 为什么分割代码时绘图不呈现?
- java - Graal在java中嵌入javascript,如何从java调用列表/数组上的地图?可能吗?
- android - 如何在布局文件中将按钮推到屏幕底部?
- java - 电子邮件的正则表达式,其间应包含连字符,但不能在开始和结束位置
- rollupjs - lerna run --parallel 不适用于汇总手表
- javascript - 使用“doMock”以避免提升时开玩笑不模拟模块
- c - extern const 数组声明/初始化错误(无效的初始化程序/冲突类型)
- git - Git 强制使用 HTTPS
- python - 检查比较值是值还是函数然后比较的 Pythonic 方法
- rust - 如何返回包含 serde_json::Value 的结果?