简体   繁体   English

当我向一个州添加多个州 <item> 在状态列表可绘制对象中,它停止工作。 为什么?

[英]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.

相关问题 在Android状态列表drawable中,忽略state_focused - In an Android state-list drawable, state_focused is ignored 当我将setOnItemSelectedListener添加到Android中的Gallery时,状态选择器停止工作。 你知道避免吗? - When I add setOnItemSelectedListener to Gallery in Android state selector stops working. Do you know to avoid it? RecyclerView项目可绘制状态不起作用 - RecyclerView Item drawable states not working 错误:发现项目Drawable / string超过一次 - Error: Found item Drawable/string more than one time android创建动态状态列表drawables - android create dynamic state-list drawables 为什么Android listView onitemclicklistner更新列表中的多个项目? - why Android listView onitemclicklistner update more than one item in the list? 当我触摸ImageView时,为什么将我的state-list-drawable应用于ImageView时,它不会将可绘制对象用于按下状态? - Why doesn't my state-list-drawable applied to an ImageView use the drawable for pressed-state when I touch the ImageView? 无法在列表视图中添加多个项目 - Unable to add more than one item in a listview 显示数组列表中的多个项目 - Display more than one item in array list 没有出现多个列表项 - More than one list item is not showing up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM