具有自定义背景和“?attr / selectableItemBackground”的按钮

Mar*_*cGV 5 android android-button android-view material-design android-attributes

如何定义Button具有自定义背景和属性的
"?attr/selectableItemBackground"

使用此代码,该"?attr/selectableItemBackground"属性将被忽略,它不会显示触摸反馈。

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:foreground="?attr/selectableItemBackground"/>
Run Code Online (Sandbox Code Playgroud)

使用此其他代码,可选择的作品,但我失去了背景色:

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"/>
Run Code Online (Sandbox Code Playgroud)

Par*_* P. 4

好吧,你可以选择parent

创建父级并赋予background白色并用作selectableItemBackground子级的背景。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)