所以。您知道当您点击 Google+ 应用中的帖子时,整个 View 会变成蓝色。我也想这样做,但要使用 ImageView。
我有以下代码 fragment ,将实际图像设置为背景,将选择器设置为主要资源。这看起来不错,但不考虑背景图像的 scaleType:
<ImageView
android:id="@+id/painting_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/img"
android:src="@drawable/selector"
android:scaleType="centerCrop" />
顺便说一下,@drawable/selector
只是一个为state_pressed
显示透明颜色的选择器:
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#44521400" />
</shape></item>
如何在遵守 scaleType 的同时完成这项工作?
最佳答案
将 ImageView
包裹在 FrameLayout
中,并将 FrameLayout
定义为可点击。小心将 onClick 事件分配给 FrameLayout
,而不是分配给 ImageView
,否则效果会中断!另请注意,foreground
已设置为选择器,而不是 background
。
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="@drawable/imagebutton_selector" >
<ImageView
android:id="@+id/painting_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop" />
</FrameLayout>
关于android - 单击 View 时在 View 上显示透明颜色突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13733964/