文章同步自 CSDN:http://blog.csdn.net/qq_24867873/article/details/76423514
在 Android 5.0 推出之后,點擊 Button 默認增加了水波紋的動畫效果。但是按照往常的方式給 Button 設置了 background 之后,發現水波紋效果沒有了。
四角尖尖,而且點擊時給人感覺很突兀。
不過到了現在我才知道是添加顏色的方式不對。
正確的改變 Button 顏色的姿勢如下:
首先在 values/styles.xml
文件中添加如下風格:
<style name="BlueButtonStyle" parent="ThemeOverlay.AppCompat">
<item name="colorButtonNormal">@android:color/holo_blue_light</item>
</style>
或者是:
<style name="RedButtonStyle" parent="Widget.AppCompat.Button.Borderless">
<item name="colorButtonNormal">@android:color/holo_red_light</item>
</style>
這兩種主題都可以,嘗試之后,發現效果一致。
在 xml 文件中使用:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Button"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:theme="@style/RedButtonStyle"/>
水波紋效果出來了。
其中有一行 android:textAllCaps="false"
可以解除 Button 對文本大寫的限制。
歡迎關注我的微信公眾號