按下时,我想要将图像从黑色背景更改为灰色背景,使用选择器似乎是最好的方法,但颜色不会改变。我可以在点击时更改图像,但我想改变背景颜色。我宁愿不使用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
任何帮助都会很棒。
谢谢, 德文
答案 0 :(得分:2)
<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
。
答案 1 :(得分:0)
对于那些好奇的人,我将ImageView
更改为ImageButton
。当我通过代码将ImageButton
的state_selected设置为true时,它会将背景颜色设置为选择器项中设置的颜色:
<item
android:state_selected="true"
android:drawable="@color/gray"/>
但是,当然,要取消选择它,您必须将state_selected设置为false。
这将通过以下方式完成:imageButton.setSelected(<true or false>);
此状态专门针对ImageButton
生成,如here所示。