android - 单击 View 时在 View 上显示透明颜色突出显示

标签 android

所以。您知道当您点击 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/

相关文章:

android - 调用 onCreate() 方法而不是 onNewIntent()

android - 在 Android 中以编程方式生成编辑文本

android - 如何在 Jetpack Compose 中让底页覆盖整个屏幕

android - 从 SpannableStringBuilder 获取 SpannableString 的最佳方法

android - 如何在 Dialog 中正确使用 FragmentResultListener

android - WebView 和 ICS 上的 UTF-8 编码

android - 以编程方式设置主题导致黑色背景

java - 在android中更新firebase数据库

android - 查找 Drawable 资源时出现 NotFoundException

java - 最后一个 block 使用 CipherInputStream/CipherOutputStream 不完整,即使使用填充 AES/CBC/PKCS5Padding