gpt4 book ai didi

android - 为什么当我触摸 ImageView 时,应用于 ImageView 的状态列表可绘制对象不使用按下状态的可绘制对象?

转载 作者:行者123 更新时间:2023-11-29 15:43:28 24 4
gpt4 key购买 nike

我有我的 ImageView在 XML 中定义如下:

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fragment_imageView"
android:src="@drawable/ic_photo"
android:background="@drawable/button_state_list_drawable" />

button_state_list_drawable.xml是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:drawable="@drawable/button_pressed_state_drawable"
android:state_pressed="true" />

<item android:drawable="@drawable/button_enabled_state_drawable"
android:state_enabled="true" />

</selector>

button_pressed_state_drawable.xml是:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<size android:width="50dp"
android:height="50dp" />

<padding android:left="25dp"
android:right="25dp"
android:top="25dp"
android:bottom="25dp" />

<stroke android:color="#fff"
android:width="1dp" />

<solid android:color="#1aafd0" />

</shape>

button_enabled_state_drawable.xml是:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<size android:width="50dp"
android:height="50dp" />

<padding android:left="25dp"
android:right="25dp"
android:top="25dp"
android:bottom="25dp" />

<stroke android:color="#fff"
android:width="1dp" />

<solid android:color="@android:color/transparent" />

</shape>

问题是当我点击/触摸 ImageView , button_pressed_state_drawable似乎没有被使用,即我看不到 <solid android:color="#1aafd0" />实际上(这基本上是两个可绘制对象之间的唯一区别),即背景颜色仍然是透明的,而不是变为蓝色。我哪里搞砸了?

最佳答案

我认为您的 ImageView 不是可点击的

尝试使用 ImageView.setClicked(true) 使其可点击。

        android:clickable="true"

还要确保您的图像资源没有覆盖您的背景。

这就是我所做的,效果很好。

    <ImageView
android:id="@+id/fragment_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_state_list_drawable"
android:clickable="true"
android:padding="20dp"
android:src="@drawable/ic_launcher"/>

关于android - 为什么当我触摸 ImageView 时,应用于 ImageView 的状态列表可绘制对象不使用按下状态的可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36783873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com