Android开发:如何为ConstraintLayout底部Guideline下方区域着色?
解决ConstraintLayout中Guideline与底部区域着色的问题
为什么你的方法没用?
Guideline是ConstraintLayout里的辅助布局标记,它只用来给其他视图提供约束参考,本身不会在屏幕上渲染任何内容,所以给它设置backgroundTint或者背景色完全不会生效。
正确实现方式
要给Guideline到屏幕底部的区域着色,你需要添加一个独立的View,把它的范围约束在Guideline和父布局底部之间,再给这个View设置背景色即可。
完整代码示例
<?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" android:background="@android:color/holo_orange_light"> <!-- 原有的底部参考线,保持不变 --> <androidx.constraintlayout.widget.Guideline android:id="@+id/bottom_guideline" android:layout_width="0dp" android:layout_height="0dp" android:orientation="horizontal" app:layout_constraintGuide_percent="0.8417" /> <!-- 新增的着色View,填充Guideline到底部的区域 --> <View android:layout_width="match_parent" android:layout_height="0dp" android:background="@android:color/holo_green_light" app:layout_constraintTop_toTopOf="@id/bottom_guideline" app:layout_constraintBottom_toBottomOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
关键说明
- 新增的
View宽度设为match_parent,高度设为0dp,通过约束自动填充Guideline和底部之间的空间 - 用
app:layout_constraintTop_toTopOf把View的顶部和Guideline对齐,app:layout_constraintBottom_toBottomOf把底部和父布局底部对齐 - 给这个View设置
android:background来实现区域着色
内容的提问来源于stack exchange,提问作者Guilherme Hepfener
相关产品推荐
相关产品推荐

