How can I modify ripple color when using ?attr/selectableItemBackground as background? Ask Question

How can I modify ripple color when using ?attr/selectableItemBackground as background? Ask Question

I've seen some SO questions and they gave some possible methods to achieve what I want. For example:

  1. colorControlHighlightstyle.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、どちらも役に立ちません。

  2. backgroundTintレイアウトで属性を使用します。

    android:backgroundTint="#5677FC"そこでを に追加しました。それでもダメです。次にと にTextView変更してみましたが、違いはありませんでした。android:backgroundTintModesrc_insrc_atop

では、?attr/selectableItemBackground背景として使用するときに波紋の色を変更するにはどうすればよいでしょうか。私は Lollipop 以上のみに焦点を当てています。よろしくお願いします!

ベストアンサー1

最終的に解決策を見つけました。android:colorControlHighlighttheme で直接使用するのではなく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_backgrounddrawable:

<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_backgrounddrawable-v21フォルダ:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ripple_black" />
</selector>

最後に、ripple_blackin 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"を設定することを忘れないでください。そうしないと、リップルが表示されません。OnClickListenerOnTouchListener

おすすめ記事