【问题标题】:View with elevation and shadow in ConstraintLayout在 ConstraintLayout 中查看高程和阴影
【发布时间】:2017-09-21 01:06:47
【问题描述】:

如何使用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>

【问题讨论】:

    标签: shadow android-constraintlayout android-elevation


    【解决方案1】:

    由于某种原因,如果您将虚拟可绘制对象作为背景,则海拔在ConstraintLayout 中起作用::

    创建一个可绘制对象:

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

    【讨论】:

    • 来这里是为了解决阴影问题,结果你的例子还展示了如何在元素上圆角,我找不到像你一样直的答案。谢谢!
    • @JesosoOrtiz 很高兴我能帮上忙(:
    • 我的确切要求!谢谢
    • 不可见容器,更改 dummyGb.xml:
    【解决方案2】:

    为了使海拔工作,您需要只需为视图设置背景颜色

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

    不管是哪种视图,
    ConstraintLayout 的子级或 ConstraintLayout 本身

    您也可以使用十六进制颜色或颜色属性。
    请注意,它不能完全透明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多