Android 使用选择器设置imageView背景问题

Android 使用选择器设置imageView背景问题,android,android-layout,android-listview,selector,Android,Android Layout,Android Listview,Selector,我在设置列表视图项目中的单击事件时遇到问题 以下是默认情况下列表的显示方式: 这是我按下红色图像(红色变为蓝色)时的外观: 这就是我按下列表项的时候: 如你所见,红色图像也变成蓝色,即使我没有按下按钮。我想要的是,当我按下列表项时,红色图像没有变化,并且保持不变 我试图将android:drawSelectorOnTop=“true”添加到listview部分的xml文件中,但没有成功。我还尝试用选择器参数修复它,但仍然没有成功 我上传了一些代码,接下来的部分在不同的xml文件中,所以不要

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

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

这是我按下红色图像(红色变为蓝色)时的外观:

这就是我按下列表项的时候:

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

我试图将
android:drawSelectorOnTop=“true”
添加到listview部分的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>

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

<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>


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

尝试使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>

尝试使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>

好的。首先,很抱歉回复太晚

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

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

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

不要放选择器。选择器基本上用于在单击特定视图的同时更改背景颜色的任何时候

是的,如果您想在用户单击时更改重画图像的颜色,则再次不要使用选择器。但只需在运行时更改图像的颜色即可

如下所示:

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

        } 
希望它能帮助你。如果它不是你想要的那么让我知道

我愿意帮助你


享受。:)好的。首先,很抱歉回复太晚

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

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

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

不要放选择器。选择器基本上用于在单击特定视图的同时更改背景颜色的任何时候

是的,如果您想在用户单击时更改重画图像的颜色,则再次不要使用选择器。但只需在运行时更改图像的颜色即可

如下所示:

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

        } 
希望它能帮助你。如果它不是你想要的那么让我知道

我愿意帮助你


享受。

您应该尝试使用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"
   />

您应该尝试使用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"
   />

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


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

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


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

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


请看“不要按下所有按钮!”

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


请看“不要按下所有按钮!”这一段。

我以前找到过这个解决方案。。我没有链接,但你必须这样做。为包含botton的linearlayout创建自定义类,然后覆盖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中的linearlayout替换为无法接收按下操作的linearlayout

编辑:
对不起,我刚才看到你正在使用RelativeLayout。。但我相信这是一样的。

我以前找到过这个解决方案。。我没有链接,但你必须这样做。为包含botton的linearlayout创建自定义类,然后覆盖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中的linearlayout替换为无法接收按下操作的linearlayout

编辑:
对不起,我刚才看到你正在使用RelativeLayout。。但我觉得还是一样。

谢谢,但没用。在selector方法中,当我单击按钮时,它保持不变,即用红色表示。在第一种方法中,我的态度与更改前相同。正如您所说的“或”,因此我更改了一次选择器,然后更改了xml-一次只更改一个方法。请注意,
focusable
focusableInTouchMode
(您有一个