我想创建一个类似一个包含按钮和标签的盒子,并带有白色稀松布,通常可以通过以下方式完成:
<relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" androID:layout_alignParentBottom=true androID:background="#99ffffff androID:gravity="center_horizontal"> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="button Text" androID:layout_margin="8dp"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Label Text" androID:layout_margin="8dp"/> </linearLayout></relativeLayout>
我所做的是:
<androID.support.constraint.ConstraintLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <VIEw androID:ID="@+ID/scrim androID:layout_wIDth="0dp" androID:layout_height="0dp" androID:background="#99ffffff" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constrainttop_totopOf="@+ID/button"/> <button androID:ID="@+ID/button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margin="8dp" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_totopOf="@+ID/label"/> <TextVIEw androID:ID="@+ID/label androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin="8dp" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintBottom_toBottomOf="parent"/></androID.support.constraint.ConstraintLayout>
这个想法是创建一个稀疏视图,该视图在按钮的底部约束到父对象,在顶部的顶部约束在顶部,一个按钮在TextVIEw之上,一个TextVIEw约束在父对象的底部.
问题是稀松布不尊重按钮的边缘,即稀松布的顶部与按钮的顶部齐平,而不是其上方的8dp.
解决方法:
您得到的结果是预期的-您告诉scrim VIEw将其顶部限制在按钮的顶部-此处不包括边距.约束条件连接到目标,并且可以具有(正)边距.边距仅适用于现有连接.
如果我们可以使用负边距,则可以在稀松布视图的顶部约束上设置一个. las,目前尚无授权.您可以做的是使用一个空间视图来模拟它-将其约束到按钮的顶部,然后将稀松布视图的顶部约束到空间视图的顶部,如下所示:
<androID.support.constraint.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"> <VIEw androID:ID="@+ID/scrim" androID:layout_wIDth="0dp" androID:layout_height="0dp" androID:background="#99ff00ff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_totopOf="@+ID/topbutton" /> <Space androID:ID="@+ID/topbutton" androID:layout_wIDth="8dp" androID:layout_height="8dp" app:layout_constraintBottom_totopOf="@+ID/button" app:layout_constraintleft_toleftOf="@+ID/button" /> <button androID:ID="@+ID/button" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_margin="8dp" androID:text="button Text" app:layout_constraintBottom_totopOf="@+ID/label" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" /> <TextVIEw androID:ID="@+ID/label" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_margin="8dp" androID:text="Label Text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintleft_toleftOf="parent" app:layout_constraintRight_toRightOf="parent" /></androID.support.constraint.ConstraintLayout>
总结 以上是内存溢出为你收集整理的android-使用ConstraintLayout创建背景全部内容,希望文章能够帮你解决android-使用ConstraintLayout创建背景所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)