在ConstraintLayout中实现带有高度和阴影的视图

13

如何使用 ConstraintLayout 在视图中显示带阴影的高程?

使用 RelativeLinear 可以在实现列表时执行具有阴影的高程,但我无法使用 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>
2个回答

23

由于某些原因,在ConstraintLayout中,如果您将一个虚拟的可绘制对象设为背景,高度设置将起作用:

创建一个 drawable:

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>

使用此作为视图的背景,并像通常一样使用高度。

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

那么你将得到:

<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"/>

4
来这里是为了解决阴影问题,结果发现你的示例还展示了如何给元素设置圆角,这正是我一直在寻找的答案。谢谢! - Jesuso Ortiz
1
@JesusoOrtiz 很高兴我能帮到你 (: - n_r
1
我的确切要求!谢谢 - Prabhakaran
不可见容器,更改dummyGb.xml:<shape android:shape="rectangle"> <solid android:color="#00000000"/> </shape> - Martin

12
为使高程(elevation)生效,您只需给一个视图设置背景颜色。
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/your_color"
    android:text="SSID"/>

无论是什么类型的View,
它都可以是ConstraintLayout的子视图或ConstraintLayout本身。

您也可以使用十六进制颜色或颜色属性。
只需注意不要完全透明即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接