当View的父布局为ConstraintLayout时,我需要View Cover Button,但这不是我期望的结果 [英] I want View cover Button when View's parent layout is ConstraintLayout,but It's not the result I expect
问题描述
这是代码内部文件activity_main.xml:
Here's the code inner file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
以下是预览:
按钮未覆盖.当我尝试用TextView替换按钮时,TextView被覆盖.
The Button is not covered.When I try replace the Button with TextView, The TextView is covered.
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="start"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="stop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
以下是预览:
在Internet上找不到任何信息.我必须用TextView替换Button吗,还有其他方法吗?
No information is found on the Internet. Do I have to replace the Button with a TextView, Is there any other way?
推荐答案
Your Button
s have a higher elevation
than the View
(and than the TextView
). That's why it shows above the View
, even when it should be drawn below by layout order.
请参见《材料设计指南》中的按钮定义:
See Buttons definition in Material Design Guidelines:
海拔
平面按钮:0dp
凸起按钮:2dp
以及海拔和高度;阴影.
因此,您可以将View
的elevation
设置为2dp
以上的值:
So, you could set the elevation
of the View
to something above 2dp
:
<View
android:elevation="3dp"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ff0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
这篇关于当View的父布局为ConstraintLayout时,我需要View Cover Button,但这不是我期望的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!