View with height and shadow in ConstraintLayout

How can I show elevations in a shadow view using ConstraintLayout ?

With Relative and Linear , elevations with shadows can be performed to implement the list, but I cannot do this with ConstraintLayout .

 <?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:orientation="vertical"> <TextView android:id="@+id/list_ssid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:elevation="8dp" android:background="#fff" android:text="SSID" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/guideline" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/list_ch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:elevation="8dp" android:background="#fff" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="CH" app:layout_constraintLeft_toLeftOf="@+id/guideline" app:layout_constraintRight_toLeftOf="@+id/guideline2" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/list_dB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:elevation="8dp" android:background="#fff" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="dB" app:layout_constraintLeft_toLeftOf="@+id/guideline2" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <android.support.constraint.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.65" tools:layout_editor_absoluteX="239dp" tools:layout_editor_absoluteY="0dp" /> <android.support.constraint.Guideline android:id="@+id/guideline2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintGuide_percent="0.83" tools:layout_editor_absoluteX="306dp" tools:layout_editor_absoluteY="0dp" /> </android.support.constraint.ConstraintLayout> 
+7
source share
2 answers

For some reason, elevation works in ConstraintLayout if you set a dummy object to draw as the background:

Create a drawing:

dummyBg.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@android:color/white"/> <corners android:radius="2dp" /> </shape> </item> </layer-list> 

Use this as the background for the view and use the elevation as usual.

  android:elevation="8dp" android:background="@drawable/dummyBg" android:padding="4dp" 

So you get:

 <TextView android:id="@+id/list_ssid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_marginStart="8dp" android:layout_marginTop="6dp" android:layout_marginBottom="2dp" android:elevation="8dp" android:background="@drawable/dummyBg" android:padding="4dp" android:text="SSID" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/guideline" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent"/> 
+8
source

In order for the height to work, you just need to set the background color for the view.

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/your_color" android:text="SSID"/> 

It doesn't matter what kind of view
child of ConstraintLayout or ConstraintLayout itself

You can also use a hex color or color attribute.

0
source

Source: https://habr.com/ru/post/1267092/


All Articles