How to change the shape of an onClick highlight?

2019-07-20 16:44发布

问题:

I currently have icons in a GridView on my app. When they are clicked, an orange square appears around them briefly as a highlight. The only problem is that I think this looks amateurish and would like to change the shape so that it clips the icon in the GridView instead of a large square. If you are unsure as to what I mean, it is carried out successfully in the Catch Notes app, on their dashboard/home screen. I was just wondering whether anybody knew a way to tackle this or if it is simply a small layout attribute.

https://play.google.com/store/apps/details?id=com.threebanana.notes&hl=en

Thanks in advance, all help would be appreciated!

It looks effectively like this EditText in the image below. The way that it borders the EditText in orange is exactly how it borders the icons in the home screen when clicked.

回答1:

First, in your layout file, specify a drawable background:

android:background="@drawable/bg_your_view"

Then edit the bg_your_view file which is in drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/bg_your_view_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/bg_your_view_normal" android:state_focused="false"></item>
</selector>

At last, create corresponding drawable files for pressed and normal states:

  • bg_your_view_pressed.xml contains the shape, the color you want for highlight.
  • bg_your_view_normal.xml is similar, just without highlight effect.


回答2:

I've decided that the easiest way to solve this problem is to simply reference another image when the image on the GridView has been clicked. This means that the second image can just be edited in Photoshop in order to have a glow around it.