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

匿名 (未验证) 提交于 2019-12-03 03:05:02

问题:

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

  1. Use colorControlHighlight attribute in styles.xml.

    Here is my styles-v21.xml:

    And my widget:

    And it doesn't work. I also tried to add parent="Theme.AppCompat to "SelectableItemBackground" style, or change to colorControlHighlight(no android: prefix)", or change to ?android:attr/selectableItemBackground, neither is useful.

  2. Use backgroundTint attribute in layout.

    So I add android:backgroundTint="#5677FC" to my TextView. Still useless. Then I tried to change android:backgroundTintMode to src_in and src_atop, and they never make a difference.

So, how can I change ripple color when I use ?attr/selectableItemBackground as background. I only focus on Lollipop and above. Thank you in advance!

回答1:

Finally I find the solution: instead of using android:colorControlHighlight directly in theme SelectableItemBackground, I should write another style:

Then:

Finally add style="@style/SelectableItemBackground" to View in layout.xml.

UPDATED ON 2016/8/26 After N's release, I found that sometimes we cannot use this method to set ripple color for some kind of View(for example, the CardView). Now I highly recommend developers using RippleDrawable, which can also be declared in xml. Here is an example:

I want to show a ripple effect when user touches/clicks a CardView above API21, and of course there should be another kind of feedback before Lollipop. So I should write:

and selectable_item_background in drawable folder:

selectable_item_background in drawable-v21 folder:

finally, the ripple_black in drawable(or drawable-v21) folder:

That's it. For other views, maybe you should use android:background="@drawable/selectable_item_background". Don't forget to set an OnClickListener, OnTouchListener or something like those for them, otherwise ripple won't show.



回答2:

harrane and Liuting are right. Accepted answer is not the best way. Let me show in code how to change ripple color for pre-Lollipop versions and higher

  1. Your AppTheme should inherit from any AppCompat theme and contain colorControlHighlight attribute (without 'android:' prefix)

     
  2. Your view should contain clickable="true" and background should be "?attr/selectableItemBackgroundBorderless" or "?attr/selectableItemBackground" :

Note: that if your parent view has white background you won't see ripple effect since it's white. Change colorControlHighlight value for a different color

Also, if you want different ripple colors on different activities you can set personal theme for each activity in Manifest file, for example:

       

UPD: What if you want different ripple colors for different fragments in the same activity?

You need to change attributes of Activity Theme for each fragment in runtime. Just overwrite them before fragment was inflated with your custom style and apply to a current Theme:

in values/styles.xml

    

Then, in your fragment before inflation in onCreateView():

    @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        getContext().getTheme().applyStyle(R.style.colorControlHighlight_blue, true); //blue ripple color        View view = inflater.inflate(R.layout.my_fragment_layout, container, false);        return view;     } 

This style will work only for this fragment

UPD 2: Please correct me if I'm wrong but seems that colorControlHighlight attribute doesn't work in layouts if you apply them to a view:

    

and works only when it is set to a Theme



回答3:

The accepted answer is wrong.

The correct way to use is what Liuting mentioned in the comment. Use colorControlHighlight instead of android:colorControlHighlight for changing the default colorControlHighlight from AppCompat

* Please refer to http://android-developers.blogspot.co.uk/2014/10/appcompat-v21-material-design-for-pre.html in the Theming section *



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!