2011-06-07 100 views
9

我有一個listView。 custom_listitem1.xml用於列表中的項目顯示。現在我想爲該項目的背景設置基於Selector的顏色資源(list_selector_color.xml)。Android Textview背景顏色資源

custom_listitem1.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:textStyle="bold" 
    android:background="@color/list_selector_color" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:minHeight="?android:attr/listPreferredItemHeight"/> 

list_selector_color.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
      android:color="#ffff0000"/> <!-- pressed --> 
    <item android:state_focused="true" 
      android:color="#ff0000ff"/> <!-- focused --> 
    <item android:color="#ff000000"/> <!-- default --> 
</selector> 

,但它給下面的錯誤.....

06-06 18:38:19.422: ERROR/AndroidRuntime(18603): Uncaught handler: thread main exiting due to uncaught exception 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603): android.view.InflateException: Binary XML file line #1: Error inflating class <unknown> 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.LayoutInflater.createView(LayoutInflater.java:513) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.LayoutInflater.inflate(LayoutInflater.java:385) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.ResourceCursorAdapter.newView(ResourceCursorAdapter.java:79) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.SimpleCursorAdapter.newView(SimpleCursorAdapter.java:96) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.CursorAdapter.getView(CursorAdapter.java:182) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.AbsListView.obtainView(AbsListView.java:1274) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.ListView.makeAndAddView(ListView.java:1668) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.ListView.fillDown(ListView.java:637) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.ListView.fillFromTop(ListView.java:694) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.ListView.layoutChildren(ListView.java:1521) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.AbsListView.onLayout(AbsListView.java:1113) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1108) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.onLayout(LinearLayout.java:920) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.LinearLayout.onLayout(LinearLayout.java:918) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.FrameLayout.onLayout(FrameLayout.java:333) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.layout(View.java:6830) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.ViewRoot.performTraversals(ViewRoot.java:996) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1633) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.os.Looper.loop(Looper.java:123) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at java.lang.reflect.Method.invoke(Method.java:521) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at dalvik.system.NativeStart.main(Native Method) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603): Caused by: java.lang.reflect.InvocationTargetException 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.TextView.<init>(TextView.java:329) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at java.lang.reflect.Constructor.constructNative(Native Method) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.LayoutInflater.createView(LayoutInflater.java:500) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  ... 52 more 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603): Caused by: android.content.res.Resources$NotFoundException: File res/color/list_selector_color.xml from drawable resource ID #0x7f070000 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.content.res.Resources.loadDrawable(Resources.java:1693) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.content.res.TypedArray.getDrawable(TypedArray.java:548) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.view.View.<init>(View.java:1850) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  at android.widget.TextView.<init>(TextView.java:335) 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603):  ... 56 more 
06-06 18:38:19.445: ERROR/AndroidRuntime(18603): Caused by: org.xmlpul 

是什麼問題在我的鱈魚è?

+0

,你必須把你的list_selector_color.xml文件在您的項目。 – Sujit 2011-06-07 13:21:56

+0

我推薦將「list_selector_color.xml」放在res/drawable文件夾中。如果不存在,請先在res目錄下創建「drawable」文件夾。 – 2011-06-07 13:27:50

回答

0

你在哪裏存儲list_selector_color.xml? 如果是在res /顏色嘗試將其搬遷到RES /繪製 並呼籲像android:background="@drawable/list_selector_color"

+0

這不起作用 - list_selector_color.xml文件導致運行時錯誤,因爲每個如果從可繪製文件夾中拉出,則需要設置android:drawable屬性集。有android:color設置是不夠的。 :\ – SilithCrowe 2012-06-05 17:50:31

0

你需要創建color.xml成... \res\values\color.xml項目目錄。
樣本color.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<color name="background">#1474c0</color> 
<color name="row">#ff0000</color> 
<color name="translucent_red">#ff0000</color> 
</resources> 

那麼如何使用顏色?請參閱由+++++++ 樣本的main.xml文件,這個例子並添加行

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
+++++++android:background="@color/translucent_red"> 
<ListView android:id="@+id/android:list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
<TextView android:id="@+id/android:empty" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="No Notes!"/> 
</LinearLayout> 
+0

我想爲TextView在不同的狀態(例如按下,焦點)顯示不同的顏色。你的方式如何? – 2011-06-08 10:23:49

0

是有可能如果您將每個textview定義爲每個LinearLayout,那麼它就起作用。

+0

您能否介紹一些代碼示例我們如何在LinearLayout的不同狀態(例如按下,焦點)設置不同的顏色? – 2011-06-24 04:48:49

7
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@color/background"//color 1 
> 
<TextView 
    android:id="@+id/tvheader" 
    android:layout_width="300dip" 
    android:layout_height="wrap_content" 
    android:background="#EC7703" 
    android:textColor="#000000" 
    android:text=" Edit Items" /> 
    /> 
<TextView 
    android:id="@+id/tvsl" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:background="@color/translucent_red"//color 2 
    /> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/background"> 
     <EditText 
     android:id="@+id/txtsl" 
     android:maxLines="1" 
     android:layout_marginTop="3dip" 
     android:layout_marginBottom="2dip" 
     android:layout_width="wrap_content" 
     android:ems="20" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 
    <TextView 
    android:id="@+id/tvname" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:background="@color/background" 
    /> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/row"> 
    <Spinner 
      android:id="@+id/txtname" 
      android:layout_width="200dip" 
      android:layout_height="wrap_content" 
      android:drawSelectorOnTop="true" 
      android:prompt="@string/medicine_info" 
      android:layout_toRightOf="@id/slcolon" 
      /> 
    </LinearLayout> 
    <TextView 
    android:id="@+id/tvqty" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:background="@color/background" 
    /> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/background"> 
    <EditText 
    android:id="@+id/txtqty" 
    android:maxLines="1" 
    android:layout_marginTop="3dip" 
    android:gravity="right" 
    android:layout_marginBottom="2dip" 
    android:layout_width="wrap_content" 
    android:ems="20" 
    android:layout_height="wrap_content"/> 
    </LinearLayout> 
    <TextView 
    android:id="@+id/tvuom" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:background="@color/background" 
    /> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/background"> 
    <EditText 
    android:id="@+id/txtuom" 
    android:maxLines="1" 
    android:layout_marginTop="3dip" 
    android:layout_marginBottom="2dip" 
    android:layout_width="wrap_content" 
    android:ems="20" 
    android:layout_height="wrap_content"/> 
    </LinearLayout> 
    <TableLayout 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:background="@color/background" 
     android:layout_gravity="center" 
     android:stretchColumns="*" > 
    <TableRow> 
    <Button 
     android:id="@+id/Btnok" 
     android:text="Edit" /> 
    <Button 
     android:id="@+id/Btndelete" 
     android:text="Delete" /> 
    <Button 
     android:id="@+id/Btncancel" 
     android:text="Cancel" /> 
    </TableRow> 
    </TableLayout> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="40dip" 
     android:background="#F7BE57">//collor change by using color code 
     <TextView 
      android:id="@+id/maincolon" 
      android:layout_width="wrap_content" 
      android:layout_height="40dip" 
      android:text="     " /> 
     <TextView 
      android:id="@+id/tvprovider" 
      android:layout_width="wrap_content" 
      android:layout_height="40dip" 
      android:textColor="#000000" 
      android:text="Provider : Softworks" 
     /> 
    </LinearLayout> 
</LinearLayout> 
+0

我需要不同的顏色處於同一視圖的不同狀態(例如按下,對焦)。設置默認顏色爲黑色,按下它將變成黃色,如果它被聚焦,則會變成紅色。你的代碼如何可能? – 2011-06-24 12:39:32

-1

現在我明白你mean.ok請看下面的例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<LinearLayout android:id="@+id/topField" android:layout_weight="2.5" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:background="@drawable/topField_design" 
    android:orientation="horizontal" > 
    <EditText android:id="@+id/digitsText" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" android:maxLines="1" android:scrollHorizontally="true" 
     android:textSize="28sp" android:freezesText="true" android:background="#F7BE57" 
     android:layout_margin="0dip" 
     android:contentDescription="description" 
     android:focusableInTouchMode="true" android:editable="true" android:cursorVisible="false" /> 
</LinearLayout> 

現在 「topField_design.xml」 是包含drowable圖像資源drowable資源XML文件。

<?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/btn_dial_textfield_pressed" /> 
<item android:state_focused="true" 
    android:drawable="@drawable/btn_dial_textfield_selected" /> 
<item 
    android:drawable="@drawable/btn_dial_textfield_normal" />//btn_dial_textfield_normal is a .png image resource into drowable folder "btn_dial_textfield_normal.png" 
</selector> 
9

您不能使用顏色選擇器作爲背景。您可以使用,而不是繪製選擇:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true"> 
     <shape> 
      <solid android:color="#ffff0000"/> 
     </shape> 
    </item><!-- pressed --> 

    <item android:state_focused="true"> 
     <shape> 
      <solid android:color="#ff0000ff"/> 
     </shape> 
    </item><!-- focused --> 

    <item> 
     <shape> 
      <solid android:color="#ff000000"/> 
     </shape> 
    </item><!-- default --> 

</selector> 

你將不得不把它放在drawable/文件夾而不是color/和訪問作爲

android:background="@drawable/list_selector_color" 
+0

老兄!我從來沒有想過在標籤內創建形狀!你是一個天才。我總是討厭我不能直接在標籤內使用android:color,因此我總是會爲每個創建單獨的文件! – LeoFarage 2015-07-26 23:03:16