web-dev-qa-db-ja.com

Android Textview background color resource

私はlistViewを持っています。 custom_listitem1.xmlは、リストに表示されるアイテムに使用されます。次に、アイテムの背景にSelectorベースのカラーリソース(list_selector_color.xml)を設定します。

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

私のコードの問題は何ですか?

9
dev_android

背景としてカラーセレクターを使用することはできません。代わりにドローアブルセレクターを使用できます。

<?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"
10
ernazm
<?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>
7
rabby

List_selector_color.xmlはどこに保存しますか? res/colorの場合は、res/drawableに再配置してAndroid:background="@drawable/list_selector_color"

0
Dmitri Gudkov

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

はい、各textviewを各LinearLayoutに定義すると、機能します。

0
rabby