在使用selectableItemBackground属性时使用自定义颜色作为按钮背景

Moh*_*der 38 android android-layout

我正在尝试使用

android:background="?android:attr/selectableItemBackground"

让我的按钮为每个Android版本做适当的效果,如涟漪等.

但是当我需要不同的颜色时,这会产生一个灰色的按钮.

如何覆盖此默认颜色?

son*_*net 10

如果您阅读了Button.java源代码,那么您将看到它是TextView.java的子.我为这个问题做了一个简单的解决方法.

<LinearLayout android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:clickable="true"
              android:background="#1f5050">

       <TextView android:layout_width="some_dp"
                 android:layout_height="some_dp"
                 android:id="@+id/button"
                 android:background="?android:selectableItemBackground" />

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

在代码中:

button.setOnClickLisetener(new Onclicklistener() {
    // do your stuff here
}
Run Code Online (Sandbox Code Playgroud)

如果有人可以扩展TextView类并使用相关功能创建自定义Button,那会好得多.

注意:我的minsdk也是14,棒棒糖涟漪效果也很好