【问题标题】:Setting imageView background issue with selector使用选择器设置 imageView 背景问题
【发布时间】:2012-04-27 02:50:04
【问题描述】:

我在设置列表视图项中的点击事件时遇到问题。

这是列表在默认情况下的显示方式:

这是当我按下红色图像时的样子(红色变成蓝色):

这是当我按下列表项时:

如你所见,红色图像也变成了蓝色,即使我没有按下按钮。我想要的是,当我按下列表项时,红色图像没有变化,并且保持不变。

我尝试将android:drawSelectorOnTop="true" 添加到列表视图部分的 xml 文件中,但没有成功。我也尝试使用选择器参数修复它,但仍然没有成功。

我上传了一些代码,接下来的部分在不同的xml文件中,所以不要混淆。

编辑:我想在android:listSelector 中使用操作系统项目列表背景颜色而不是我自己的颜色,因为我已经这样做了。

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/blue" />
    <item android:drawable="@color/red"/> 

</selector>

-----------新文件---------- ----------

<ListView android:id="@+id/list_theme"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_above="@id/contbtn_themelist"
    android:stackFromBottom="false"
    android:transcriptMode="disabled"/>

-----------新文件---------- ----------

<RelativeLayout android:id="@+id/edit_back"
    android:layout_width="80dp"
    android:layout_height="77dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:background="@drawable/edit_skin_selector2"  >

    <ImageView
        android:id="@+id/skinEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:src="@android:drawable/ic_menu_edit"
        android:layout_centerInParent="true" />
</RelativeLayout>

编辑: row.xml

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:paddingBottom="5dip"
        android:paddingLeft="2dip"
        android:paddingRight="2dip"
        android:paddingTop="5dip" >

        <ImageView
            android:id="@+id/itemlist_checkedd"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_checkitem" />

        <ImageView
            android:id="@+id/skinpreview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/itemlist_checkedd"
            android:scaleType="fitXY" />

        <RelativeLayout android:id="@+id/edit_back"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/edit_skin_selector2"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true">

            <ImageView 
                android:id="@+id/skinEdit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@android:drawable/ic_menu_edit"
                android:layout_centerInParent="true" />
        </RelativeLayout>

        <ImageView
            android:id="@+id/separator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dip"
            android:layout_toLeftOf="@id/edit_back"
            android:src="@drawable/separator" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="15dip"
            android:layout_toLeftOf="@id/separator"
            android:layout_toRightOf="@id/skinpreview"
            android:textAppearance="?android:attr/textAppearanceLarge" />

 </RelativeLayout>

任何帮助都会很棒,谢谢。

【问题讨论】:

  • 所以你想要的是,如果用户点击列表项然后动作发生但红色图像没有变成红色,如果用户点击红色图像然后它变成红色和动作发生。我对吗 ?请回复此问题,以便我为您提供帮助。
  • @iDroid Explorer 基本上你是对的,唯一的区别是如果红色变成什么东西,它会不同于红色。另一件事,我想使用操作系统列表项的颜色,意思是某些系统中的橙色和其他系统中的蓝色。谢谢。
  • @Ok 然后等待我的回答。让我知道你在这方面取得了多大的成功?
  • 请同时放入您用于对行项目进行膨胀的row.xml文件。
  • 如果您想对图像使用默认操作系统列表项颜色而不是红色,则使用“@android:drawable/list_selector_background”作为该图像的背景。

标签: android android-layout android-listview selector


【解决方案1】:

尝试让 imageView 可点击...

<ImageView
        android:id="@+id/skinEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:src="@android:drawable/ic_menu_edit"
        android:layout_centerInParent="true"
           android:focussable="false"
           android:focussableInTouchMode="false"
           android:clickable="true" />

或者..

将选择器状态更改为

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@color/blue" />
    <item android:drawable="@color/red"/> 

</selector>

【讨论】:

  • 谢谢,但没用。在我单击按钮时的选择器方法中,它保持不变,表示为红色。在第一种方法中,我得到了与改变之前相同的态度。
  • 就像你说的“OR”,所以我改变了一次选择器,然后改变了 xml - 一次只有一种方法。
  • 次要注意,focusablefocusableInTouchMode 两个词中有一个错字(两者都有一个额外的 's')
【解决方案2】:

好的。首先很抱歉回复晚了。

在继续解决之前,让我们看看我从你的问题中得到了什么。

您想要的是,当您单击列表项时,红色图像不会转换为蓝色,如果用户单击红色图像,则它应该转换为红色。但不是整个列表项对吗???

如果是这样,那就没有什么可做的了。

不要放置选择器。选择器基本上适用于,只要您想在单击该特定视图时更改背景颜色。

是的,如果您想在用户单击 RedImage 时更改它的颜色,那么再次不要在那里使用选择器。但只需在运行时更改该图像的颜色即可。

如:

@Override 
        public void onClick(View view) { 
                 YOUR_VIEW.setBackgroundColor(0xffff0000);

        } 

希望对您有所帮助。如果它不能按您的意愿工作,请告诉我。

我愿意帮助你。

享受吧。:)

【讨论】:

  • 谢谢,但还是不行,现在按下按钮时背景没有上色。
  • 你的意思是,如果你点击红色按钮,那么整个列表项会得到改变颜色??
  • @OfiRAN:我知道你现在的问题了。但没有得到你想要的??请说明您的实际需求,以便我们为您提供适当的帮助。
  • 让我澄清一下我想要什么。在第一个图像(红色背景)中,这是没有触摸的时候,或者换句话说,这是默认状态。然后用户触摸按钮(红色背景)背景变为蓝色(第二张图片),到目前为止一切正常。接下来是有问题的状态。当用户触摸列表项时,项目的背景和按钮的背景都是彩色的(第三张图片),我想要的是只有项目的背景会改变他的颜色,并且按钮将保持默认状态,意思是红色。
  • 我已经澄清了我的问题。
【解决方案3】:

您应该尝试使用android:duplicateParentState="false" 来指示不要为其子级使用父级的可绘制状态。

<ImageView
    android:id="@+id/skinEdit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:scaleType="fitXY"
    android:src="@android:drawable/ic_menu_edit"
    android:layout_centerInParent="true" 

    android:duplicateParentState="false"
   />

更新

使您的按钮的直接父级不可点击,即在 row.xml 中添加 android:clickable="false" 并查看是否可以解决问题。现在点击项目不会改变按钮状态。但我猜点击按钮应该仍然有效。

更新

        <ImageButton 
            android:id="@+id/skinEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:src="@android:drawable/ic_menu_edit"
            android:scaleType="center"
            android:background="@android:drawable/state_redblue_drawable"
            android:padding=10dp
            android:clickable="true"/>

【讨论】:

  • 谢谢,但还是不好,没有任何改变。
  • 感谢您为我提供的帮助,但仍然不好,仍然是同样的问题。这让我发疯了......
  • 还是一样。当我按下该项目时,按钮的背景显示为。
  • 如果您希望按钮的背景大于按钮图像,请使用 ImageButton。将图像设置为 src。并将状态可绘制设置为背景..也可以有一些填充。
  • 我也试过了,但是由于某种原因,当我更改它时,我无法按下列表项,这意味着它无法按下。有什么建议吗?
【解决方案4】:

你用什么来连接列表一个simpleAdapter。要完成您所说的,需要一个扩展 BaseAdapter 的新类并更改该类的 getView 事件的背景图像。

然后在您的主要活动上,您可以设置一个 onlistItemClick 来处理列表项的点击事件。

【讨论】:

    【解决方案5】:

    我也遇到了这个问题,我用这里的技术解决了:

    http://android.cyrilmottier.com/?p=525

    看看“不要按所有东西!”这一段。

    【讨论】:

    • 为避免为任何其他视图(RelativeLayoutFrameLayout...)编写新的自定义布局,请查看my solution,它更简单。
    【解决方案6】:

    我之前找到了这个解决方案。我没有链接,但你必须这样做。为包含您的按钮的线性布局创建一个自定义类,然后覆盖 setPressed 状态以不执行任何操作;

    public class UnpresseableLinearLayout extends LinearLayout
    {
        public UnpresseableLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
    @Override
        public void setPressed(boolean pressed)
        {
            // Do nothing here. Specifically, do not propagate this message along
            // to our children so they do not incorrectly display a pressed state
            // just because one of their ancestors got pressed.
        }
    }
    

    这将解决问题,因为 android:duplicateParentState="false" 在这种情况下将不起作用。

    然后用这个替换你的 xml 中的线性布局,它不能接收新闻操作

    编辑: 抱歉,我刚刚看到您使用的是 RelativeLayout.. 但我想那是一样的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多