gpt4 book ai didi

android - 将引用选择器的背景颜色更改为 ImageView 的图像源

转载 作者:行者123 更新时间:2023-11-29 21:06:37 27 4
gpt4 key购买 nike

我有一张图片,我想在按下时将其从黑色背景更改为灰色背景,使用选择器似乎是最好的方法,但颜色没有改变。我可以通过单击更改图像,但我只想更改背景颜色。我宁愿不使用 onClickListener 只是 b/c 这看起来更干净。这是我的代码:

图片:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<RelativeLayout
android:id="@+id/image_block"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/image_selector"/>
</RelativeLayout>
</LinearLayout>

选择器:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/image_gray"
android:background="@color/gray"/>
<item
android:drawable="@drawable/image_gray"
android:background="@color/black"/>
</selector>

StackOverflow 发生时的 LogCat(彻底修复):

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.package/com.ex.package.ImageActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at dcom.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:375)
at android.app.Activity.setContentView(Activity.java:1997)
at com.ex.package.ImageActivity.onCreate(ImageActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5312)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
a

t android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
             at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
             at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:375)
             at android.app.Activity.setContentView(Activity.java:1997)
             at com.ex.package.ImageActivity.onCreate(ImageActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:5312)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.StackOverflowError
at android.util.LongSparseArray.get(LongSparseArray.java:111)
at android.util.LongSparseArray.get(LongSparseArray.java:102)
at android.content.res.Resources.getCachedDrawable(Resources.java:2226)
at android.content.res.Resources.loadDrawable(Resources.java:2086)
at android.content.res.Resources.getDrawable(Resources.java:711)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2130)
at android.content.res.Resources.getDrawable(Resources.java:711)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2130)
at android.content.res.Resources.getDrawable(Resources.java:711)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.l

任何帮助都会很棒。

谢谢,德文

最佳答案

<item>没有属性 android:background .其中的所有字段 are listed here .

如果您希望背景在点击项目时发生变化,请更改 ImageView 的 android:src@drawable/image_gray并更改其 android:background一个只在 @color/gray 之间切换的选择器和 @color/black用于其主要绘图。

它看起来像这样:

  <ImageView
android:id="@+id/image_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:background="@drawable/background_selector"
android:src="@drawable/image_gray"/>

其中 background_selector.xml 类似于:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@color/gray"/>
<item
android:drawable="@color/black"/>
</selector>

另请注意,如果您的 ImageView是可点击的考虑将其更改为 ImageButton .

关于android - 将引用选择器的背景颜色更改为 ImageView 的图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332114/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com