Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want View cover Button when View's parent layout is ConstraintLayout,but It's not the result I expect

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>

Here's the preview:

this is preview

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>

Here's the preview:

this is preview1

No information is found on the Internet. Do I have to replace the Button with a TextView, Is there any other way?

like image 951
Star Avatar asked Dec 27 '17 06:12

Star


People also ask

How do I fix this view is not constrained?

To resolve this error, you need to add constraints to the Button view that fulfills the minimum requirement of the ConstraintLayout view. You can do this in two ways: Using the Infer Constraints option from the Design window's toolbar. Using the Constrain option from the View 's context menu.

How do I change ConstraintLayout to LinearLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

Can we use linear layout in ConstraintLayout?

You can create linear layouts now with ConstraintLayout by constraining the sides of each element with each other. The quick way of creating these layouts is to select all the views together and right click to center horizontally or vertically.


2 Answers

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:

Elevation

Flat buttons: 0dp

Raised buttons: 2dp

And also the chapter on Elevation & Shadows.

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"
    />
like image 124
Xavier Rubio Jansana Avatar answered Sep 24 '22 23:09

Xavier Rubio Jansana


Try this you need to set android:elevation="" to your View like below code

       <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:elevation="1dp"
        android:text="stop"
        android:gravity="center"
        android:textColor="@color/colorWhite"
        android:textSize="15dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <View
        android:elevation="4dp"
        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>

OUTPUT

enter image description here

like image 25
AskNilesh Avatar answered Sep 22 '22 23:09

AskNilesh