Android 单击视图时在视图上显示透明颜色高光
所以。你知道当你点击Google+应用程序中的帖子时,整个视图会变成蓝色。我也想这样做,但要有一个ImageView 我有以下代码片段,将实际图像设置为背景,将选择器设置为主要资源。这看起来不错,但不考虑背景图像的scaleType:Android 单击视图时在视图上显示透明颜色高光,android,Android,所以。你知道当你点击Google+应用程序中的帖子时,整个视图会变成蓝色。我也想这样做,但要有一个ImageView 我有以下代码片段,将实际图像设置为背景,将选择器设置为主要资源。这看起来不错,但不考虑背景图像的scaleType: <ImageView android:id="@+id/painting_image" android:layout_width="match_parent" android
<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" />
如何在尊重scaleType的同时实现这一点?scaleType只应用于src drawable,而不是后台。考虑使用第二个选项,用一个覆盖视图来实现点击动作,像这样:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/painting_image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop" />
<View
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/imagebutton_selector"
android:onClick="onImageClick" />
</FrameLayout>
将您的
图像视图
包装在框架布局
中,并定义可单击的框架布局。小心将onClick事件指定给FrameLayout
,而不是ImageView
,否则效果会中断!还请注意,前台
已设置为选择器,而不是后台
<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>
您好,谢谢您的建议!但我已经回答了我自己的问题。我忘了将onClick事件设置为包装FrameLayout
;它仍然是为图像视图定义的。。。更好!例如,imagebutton_selector.xml:
<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>