2012-01-10 40 views
13

我想設置圖像視圖的背景顏色。Android:使用選擇器設置圖像視圖的背景顏色

<ImageView 
    android:id="@+id/my_image" 
    android:src="@drawable/my_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:layout_weight="1" 
    android:clickable="true" 
    android:layout_alignParentLeft="true" 
    android:background="@drawable/selector_image_view" /> 

我正在使用以下選擇器。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
<item android:state_selected="true" android:background="@color/Violet" />  
<item android:state_pressed="true" android:background="@color/red" /> 
<item android:background="@color/white" /> 
</selector> 

但是,如果我嘗試在運行時異常中使用選擇器,它會崩潰。我究竟做錯了什麼?

我logcat的

E/AndroidRuntime(4196): FATAL EXCEPTION: main 
E/AndroidRuntime(4196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.X.XCamera/com.X.XCamera.XCameraActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown> 
E/AndroidRuntime(4196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
E/AndroidRuntime(4196): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
E/AndroidRuntime(4196): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
E/AndroidRuntime(4196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
E/AndroidRuntime(4196): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(4196): at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime(4196): at android.app.ActivityThread.main(ActivityThread.java:4627) 
E/AndroidRuntime(4196): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(4196): at java.lang.reflect.Method.invoke(Method.java:521) 
E/AndroidRuntime(4196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
E/AndroidRuntime(4196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
E/AndroidRuntime(4196): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(4196): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown> 
E/AndroidRuntime(4196): at android.view.LayoutInflater.createView(LayoutInflater.java:513) 
E/AndroidRuntime(4196): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.rInflate(LayoutInflater.java:621) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.inflate(LayoutInflater.java:407) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
E/AndroidRuntime(4196): at com.X.XCamera.Settings.<init>(Settings.java:52) 
E/AndroidRuntime(4196): at com.X.XCamera.XCameraActivity.onCreate(XCameraActivity.java:92) 
E/AndroidRuntime(4196): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
E/AndroidRuntime(4196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
E/AndroidRuntime(4196): ... 11 more 
E/AndroidRuntime(4196): Caused by: java.lang.reflect.InvocationTargetException 
E/AndroidRuntime(4196): at android.widget.ImageView.<init>(ImageView.java:108) 
E/AndroidRuntime(4196): at java.lang.reflect.Constructor.constructNative(Native Method) 
E/AndroidRuntime(4196): at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 
E/AndroidRuntime(4196): at android.view.LayoutInflater.createView(LayoutInflater.java:500) 
E/AndroidRuntime(4196): ... 22 more 
E/AndroidRuntime(4196): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/selector_image_view.xml from drawable resource ID #0x7f02000e 
E/AndroidRuntime(4196): at android.content.res.Resources.loadDrawable(Resources.java:1697) 
E/AndroidRuntime(4196): at android.content.res.TypedArray.getDrawable(TypedArray.java:601) 
E/AndroidRuntime(4196): at android.view.View.<init>(View.java:2045) 
E/AndroidRuntime(4196): at android.widget.ImageView.<init>(ImageView.java:112) 
E/AndroidRuntime(4196): ... 26 more 
E/AndroidRuntime(4196): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable 
E/AndroidRuntime(4196): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:167) 
E/AndroidRuntime(4196): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787) 
E/AndroidRuntime(4196): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728) 
E/AndroidRuntime(4196): at android.content.res.Resources.loadDrawable(Resources.java:1694) 
E/AndroidRuntime(4196): ... 29 more 
W/ActivityManager(1707): Force finishing activity com.X.XCamera/.XCameraActivity 
I/IMGSRV (1707): gralloc.c:803: gralloc_device_alloc: Allocated a new surface: 
I/IMGSRV (1707):  width -> 800 
I/IMGSRV (1707):  height -> 382 
I/IMGSRV (1707):  format -> 1 (HAL_PIXEL_FORMAT_RGBA_8888) 
+1

你可以發表你的logcat的? – Hiral 2012-01-10 08:29:16

回答

36

你的logcat:

E/AndroidRuntime(4196):產生的原因: org.xmlpull .v1.XmlPullParserException:二進制XML文件行#3:標記 需要'可繪製'屬性或子標記定義可繪製

LogCat指出您的<item>標籤應包含android:drawable標籤。

您選擇的文件應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true" android:drawable="@color/Violet" />  
    <item android:state_pressed="true" android:drawable="@color/red" /> 
    <item android:drawable="@color/white" /> 
</selector> 
14

不能在選擇file.Instead使用背景標籤,你可以用它喜歡:

在 「值」 文件夾中創建一個color.xml在 「資源」 文件夾:

color.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <drawable name="color_one">#110000</drawable> 
    <drawable name="color_two">#001111</drawable> 
    <drawable name="color_three">#001100</drawable>  
</resources> 

2.現在,使用此文件在你選擇的文件,如:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_selected="true" android:drawable="@drawable/color_one" />  
    <item android:state_pressed="true" android:drawable="@drawable/color_two" /> 
    <item android:drawable="@drawable/color_three"/> 
</selector> 
+2

應該不是而不是?我對這個答案不確定,所以我把這個詞作爲一個問題而不是一個更正。如果你能澄清,我將不勝感激。謝謝。 – 2013-06-23 21:31:12

16

最簡單的方法做你想做的是增加一個子節點的項目,而不是使用其drawable屬性:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true"> 
     <color android:color="@color/Violet" /> 
    </item> 
    <item android:state_pressed="true"> 
     <color android:color="@color/red" /> 
    </item> 
    <item> 
     <color android:color="@color/white" /> 
    </item> 
</selector> 

這裏您也可以使用顏色的直接值(例如,#FFFF0000而不是「@ color/red」)以免讓您定義多個顏色資源。

1

實現選擇器的最佳方式是使用xml,而不是使用編程方式,因爲使用xml更容易實現。

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_selected="true"> 
    <color android:color="@color/Violet" /> 
</item> 
<item android:state_pressed="true"> 
    <color android:color="@color/red" /> 
</item> 
<item> 
    <color android:color="@color/white" /> 
</item> 

    </selector> 

欲瞭解更多信息,我實現了使用這個鏈接 http://www.blazin.in/2016/03/how-to-use-selectors-for-botton.html

0
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:drawable="@color/black_mediam" /> 
    <item android:state_pressed="true" android:drawable="@color/black_mediam" /> 
    <item android:drawable="@color/menu_bg" /> 
</selector> 

它工作的ImageView和按鈕

 <Button 
      android:layout_weight="1" 
      android:background="@drawable/image_selecter" 
      android:id="@+id/btn_delete_gif" 
      android:textAllCaps="false" 
      android:text="Yes" 
      android:textSize="@dimen/_13sdp" 
      android:textColor="@android:color/white" 
      android:layout_width="175dp" 
      android:layout_height="wrap_content" /> 


    <ImageView 
      android:padding="@dimen/_3sdp" 
      android:background="@drawable/image_selecter" 
      android:layout_weight="1" 
      android:layout_alignParentRight="true" 
      android:id="@+id/delete_gif_giflistview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_delete_gif" 
      android:layout_marginLeft="1dp" /> 
相關問題