I've seen some SO questions and they gave some possible methods to achieve what I want. For example:
colorControlHighlight
style.xml 内の属性を使用します。私のstyles-v21.xmlは次のとおりです:
<style name="SelectableItemBackground"> <item name="android:colorControlHighlight">#5677FC</item> <item name="android:background">?attr/selectableItemBackground</item> </style>
そして私のウィジェット:
<TextView android:id="@+id/tv_take_photo_as_bt" android:layout_width="280dp" android:layout_height="48dp" android:text="@string/act_take_photo" style="@style/SelectableItemBackground"/>
そして、それは機能しません。また、
parent="Theme.AppCompat
「SelectableItemBackground」スタイルに を追加したり、 に変更したりcolorControlHighlight(no android: prefix)"
、 に変更したりしてみましたが?android:attr/selectableItemBackground
、どちらも役に立ちません。backgroundTint
レイアウトで属性を使用します。android:backgroundTint="#5677FC"
そこでを に追加しました。それでもダメです。次にと にTextView
変更してみましたが、違いはありませんでした。android:backgroundTintMode
src_in
src_atop
では、?attr/selectableItemBackground
背景として使用するときに波紋の色を変更するにはどうすればよいでしょうか。私は Lollipop 以上のみに焦点を当てています。よろしくお願いします!
ベストアンサー1
最終的に解決策を見つけました。android:colorControlHighlight
theme で直接使用するのではなくSelectableItemBackground
、別のスタイルを記述する必要があります。
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/ripple_color</item>
</style>
それから:
<style name="SelectableItemBackground">
<item name="android:theme">@style/SelectableItemTheme</item>
<item name="android:background">?attr/selectableItemBackground</item>
</style>
最後にlayout.xmlstyle="@style/SelectableItemBackground"
に追加します。View
2016/8/26更新N のリリース後、このメソッドを使用して、ある種のView
(たとえばCardView
)のリップル カラーを設定できないことがあることがわかりました。現在、開発者には を使用することを強くお勧めしますRippleDrawable
。 も xml で宣言できます。次に例を示します。
ユーザーが上記の API21 をタッチ/クリックしたときに波及効果を表示したいのですCardView
が、もちろん Lollipop の前に別の種類のフィードバックが必要です。そのため、次のように記述する必要があります。
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foreground="@drawable/selectable_item_background"/>
およびフォルダselectable_item_background
内drawable
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:drawable="@color/color_clicked" />
</selector>
selectable_item_background
でdrawable-v21
フォルダ:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ripple_black" />
</selector>
最後に、ripple_black
in drawable
(またはdrawable-v21
) フォルダー:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/color_clicked"
tools:ignore="NewApi" /> <!--you can remove this line if it's in v21 folder-->
以上です。他のビューでは、 を使用する必要があるかもしれません。 、またはそれに類似するものandroid:background="@drawable/selectable_item_background"
を設定することを忘れないでください。そうしないと、リップルが表示されません。OnClickListener
OnTouchListener