[英]When I add more than one states to an <item> in a state-list drawable, it stops working. Why?
What I want: 我想要的是:
An ImageView with a transparent background. 具有透明背景的ImageView。 When it is clicked, its background should turn blue, and should remain blue until something else is clicked. 单击它时,其背景应变为蓝色,并应保持蓝色直到单击其他内容。 I am using state-list drawable for it. 我正在使用状态列表可绘制。
What is happening: 怎么了:
The code is given as follows. 代码如下。 The problem is that the background does turn blue, but it does not remain blue. 问题是背景确实变成了蓝色,但它并没有保持蓝色。 So I thought it is because android:state_pressed
just represents the moment when the widget is being clicked. 因此,我认为这是因为android:state_pressed
只是代表那一刻被点击的小部件时。 So I also added android:state_selected="true"
to button_state_list_drawable.xml
right after android:state_pressed="true"
. 所以我还添加android:state_selected="true"
到button_state_list_drawable.xml
之后android:state_pressed="true"
。 What happened then is that the background stopped turning blue even when the imageview is being clicked. 发生了什么事,然后是后台停止转动的蓝色被点击的ImageView时也是如此。
How do I fix this? 我该如何解决?
ImageView
defined in XML like this: 用XML定义的ImageView
像这样:
<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"
android:clickable="true" />
The button_state_list_drawable.xml
is: 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"
android:state_selected="true" />
<item android:drawable="@drawable/button_enabled_state_drawable" />
</selector>
The button_pressed_state_drawable.xml
is: 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>
The button_enabled_state_drawable.xml
is: 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>
I don´t know if this is the solution, but I have to show some code. 我不知道这是否是解决方案,但是我必须显示一些代码。 You haven´t added a default state to your selector and I can´t see your selected state (you wrote it in yur question, but you haven´t posted it). 您尚未向选择器添加默认状态,而我看不到您选择的状态(您在问题中写下了它,但尚未发布)。 It must be something like this: 一定是这样的:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_enabled_state_drawable"
android:state_enabled="true" />
<item android:drawable="@drawable/button_pressed_state_drawable"
android:state_pressed="true" />
<item android:drawable="@drawable/your_selected_state_drawable"
android:state_selected="true" />
<item android:drawable="@drawable/your_button_default_drawable"/>
</selector>
And I think you have to add programmatically the selected state (I´m not sure for this or if it does it automatically) 而且我认为您必须以编程方式添加所选状态(我不确定是否要自动添加)
yourImageView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
if(yourImageView.isSelected()){
yourImageView.setSelected(false);
}else{
yourImageView.setSelected(true);
}
}
});
I recently had this problem, and this is what I did. 我最近遇到了这个问题,这就是我所做的。
In your button_state_list_drawable.xml, add this somewhere before you default state item: 在您的button_state_list_drawable.xml中,将其添加到默认状态项目之前的某个位置:
<item android:drawable="@drawable/button_pressed_state_drawable"
android:state_selected="true"
android:state_pressed="false" />
Also remove android:state_selected="true"
from your state_pressed
item. 还要从state_pressed
项目中删除android:state_selected="true"
。
Then in the beginning of your click listener for the ImageView
, write 然后,在您的ImageView
点击监听器的开头,编写
yourImageView.setSelected(true);
This had solved my problem. 这解决了我的问题。 Hope it helps you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.