当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

查看:74
本文介绍了当View的父布局为ConstraintLayout时,我需要View Cover Button,但这不是我期望的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码内部文件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?

推荐答案

您的Button具有更高的

Your Buttons 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

以及海拔和高度;阴影.

因此,您可以将Viewelevation设置为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆