创建一个包含图像和文本等的 ViewGroup(例如 LinearLayout/RelativeLayout/etc)。
使这个 ViewGroup 可点击(例如,通过为其分配一个 OnClicListener)。
为该 ViewGroup 分配一个背景可绘制对象。
确保此背景可绘制对象是状态列表可绘制对象:
https ://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html
http://developer.android.com/guide/topics/resources/ drawable-resource.html#StateList
将适当的可绘制对象分配给各种状态(最重要的是 state_pressed,但您可能还想处理其他状态)。
当用户按下按钮时,将显示适当的可绘制对象,并且看起来好像 ViewGroup 是一个按钮(可以按下的东西)。
OP 显示按下状态的新图像后更新:
添加一个位于图像/文本/等之上的视图,其背景具有 StateListDrawable:
<RelativeLayout >
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/somerealpngfile"
...
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
...
/>
<View
android:id+"@+id/clickable_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selectable_background"
android:clickable="true"
...
/>
</RelativeLayout>
res/drawable/selectable_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/grid_state_pressed"/>
<item android:state_focused="true" android:drawable="@color/grid_state_focused"/>
<item android:drawable="@android:color/transparent"/>
</selector>
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<color name="grid_state_pressed">#BB7dbcd3</color>
<color name="grid_state_focused">#777dbcd3</color>
...
</resources>
其中颜色grid_state_pressed
和grid_state_focused
是半透明的(即它们的 alpha 小于 255)。
当用户单击您的“按钮”时,ViewR.id.clickable_view
将处理 onClick 并更改其背景颜色,从而导致图像和文本以半透明的方式闪耀。