【发布时间】:2018-04-28 08:46:10
【问题描述】:
如果 ImageButton 或 Button 放在带背景的 View 内,selectableItemBackgroundBorderless 将不起作用,但 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>
【问题讨论】:
-
发生这种情况是因为父级即 ConstraintLayout 具有背景色。
-
在你的 ImageButton 中试试这个:android:background="?attr/selectableItemBackgroundBorderless"
-
@BhavikMakwana 它发生事件,没有父母背景。
"?attr/selectableItemBackgroundBorderless"和?android:attr/selectableItemBackgroundBorderless没有任何作用。 -
似乎我也有类似的问题:issuetracker.google.com/issues/111819099。请考虑给它加星标,并尝试使用我稍后编写的解决方法的示例项目。
标签: android android-layout android-constraintlayout