简体   繁体   English

如何获取赋予ImageView的可绘制对象的资源ID

[英]How to get the resource id of a drawable given to an ImageView

I need to get the id of the drawable I assigned to an ImageView by xml. 我需要获取我通过xml分配给ImageView的可绘制对象的ID。

I have different buttons that need to change look when pressed, so I wanted to write just one method for all of them putting into 2 arrays the NOT_TOUCHED drawable ids and the TOUCHED drawable ids and so on... 我有不同的按钮需要在按下时改变外观,因此我只想为所有这些按钮编写一个方法,即可将NOT_TOUCHED可绘制ID和TOUCHED可绘制ID放入2个数组中,依此类推...

How can I do this? 我怎样才能做到这一点?

If you want to change the image view to different state when image view touch or not touch, you better use drawable selector resource. 如果要在触摸或不触摸图像时将图像视图更改为其他状态,则最好使用可绘制的选择器资源。 Create a drawable/background.xml like following, then set as background of your image view. 创建如下的drawable / background.xml,然后将其设置为图像视图的背景。

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

<item android:drawable="@drawable/focused_image" android:state_focused="true"></item>
<item android:drawable="@drawable/selected_image" android:state_selected="true"></item>
<item android:drawable="@drawable/normal_image"></item>

</selector>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM