当View的父布局为ConstraintLayout时,我想要View cover Button,但这不是我期望的结果

Sta*_*tar 5 android android-layout android-constraintlayout

这是内部文件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>
Run Code Online (Sandbox Code Playgroud)

这是预览:

这是预览

Button 未被覆盖。当我尝试用 TextView 替换 Button 时,TextView 被覆盖。

<?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>
Run Code Online (Sandbox Code Playgroud)

这是预览:

这是预览1

在互联网上找不到任何信息。我是否必须用 TextView 替换 Button,还有其他方法吗?

Xav*_*ana 3

你的s比(和)Button更高。这就是为什么它显示在 上方,即使它应该按布局顺序绘制在下方。elevationViewTextViewView

请参阅《材料设计指南》中的按钮定义:

海拔

平面按钮:0dp

凸起按钮:2dp

还有关于海拔和阴影的章节。

因此,您可以将 的 设置elevationView上面的值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"
    />
Run Code Online (Sandbox Code Playgroud)