ConstraintLayout中的selectableItemBackgroundBorderless问题

时间:2018-04-28 08:46:10

标签: android android-layout android-constraintlayout

如果将ImageButton或Button放置在具有背景的selectableItemBackgroundBorderless内,则{p> View无效,但selectableItemBackground仍可正常工作。

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"
    android:background="@color/colorAccent">

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />


    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/previous_ib"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_previous"
        app:layout_constraintBottom_toBottomOf="@+id/play_ib"
        app:layout_constraintEnd_toStartOf="@+id/play_ib"
        app:layout_constraintTop_toTopOf="@+id/play_ib" />


    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/play_ib"
        android:layout_width="68dp"
        android:layout_height="68dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view" />


    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/next_ib"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_next"
        app:layout_constraintBottom_toBottomOf="@+id/play_ib"
        app:layout_constraintStart_toEndOf="@+id/play_ib"
        app:layout_constraintTop_toTopOf="@+id/play_ib" />

</android.support.constraint.ConstraintLayout>

这就是我想要做的。但涟漪效应消失了。 selectableItemBackgroundBorderless  not working in ConstraintLayout

6 个答案:

答案 0 :(得分:1)

这是因为android:background="@color/colorPrimary"中的View。但是你真的需要这个没有任何儿童观点的观点吗? 我建议将View替换为另一个ConstraintLayout。只需将所有ImageButton放入第二个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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" >


    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/previous_ib"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="32dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_previous"
        app:layout_constraintBottom_toBottomOf="@+id/play_ib"
        app:layout_constraintEnd_toStartOf="@+id/play_ib"
        app:layout_constraintTop_toTopOf="@+id/play_ib" />


    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/play_ib"
        android:layout_width="68dp"
        android:layout_height="68dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_play"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view" />


    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/next_ib"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_next"
        app:layout_constraintBottom_toBottomOf="@+id/play_ib"
        app:layout_constraintStart_toEndOf="@+id/play_ib"
        app:layout_constraintTop_toTopOf="@+id/play_ib" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

答案 1 :(得分:1)

通常这是一个评论,但我显然太新了。

你试过试试吗? android:foreground="?selectableItemBackgroundBorderless"

而不是
android:background="?selectableItemBackgroundBorderless"

答案 2 :(得分:1)

对于那些正在寻找合理答案的人,恐怕这是ConstraintLayoutselectableItemBackground shown upon clicking, while selectableItemBackgroundBorderless isn't中的一个已知错误,因为它已经提到了here

请考虑加注星标,并在发现更多问题时进行举报。

答案 3 :(得分:1)

我用了它,对我来说很好用。

android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"

答案 4 :(得分:0)

在此问题解决之前,一种解决方案是使用 android:background="?actionBarItemBackground"会产生圆形波纹效果 并且还可以在约束布局中使用。

答案 5 :(得分:0)

如果您的父视图是 ConstraintLayout,那么请将 ImageButton 包裹在 FrameLayout 中,例如:

<FrameLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintBottom_toBottomOf="@+id/play_ib"
          app:layout_constraintStart_toEndOf="@+id/play_ib"
          app:layout_constraintTop_toTopOf="@+id/play_ib">

   <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/next_ib"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:background="?selectableItemBackgroundBorderless"
        android:contentDescription="@string/app_name"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_next" />

</FrameLayout>

约束布局中显然存在一些导致这种行为的错误。如果您好奇,可以探索更多。