设置文本和列表视图的背景颜色,listview不会使用setbackground更改颜色

时间:2011-08-17 01:59:30

标签: android-layout

我有listview和text视图的视图。我想为textview和listview设置diff颜色。还列出了几个没有填满整个屏幕的项目。

需要帮助来解决问题            

<TextView android:id="@+id/textView1" android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_weight="0.12" android:layout_width="match_parent"
    android:text="@string/songsbyyear" android:gravity="center"
    android:textColor="@color/white" android:textSize="22dp"
    android:background="@color/steelblue" android:textStyle="bold"></TextView>
<ListView android:layout_width="wrap_content"
    android:drawSelectorOnTop="false" android:id="@android:id/list"
    android:background="@color/darkslateblue"  android:layout_height="456dp">    </ListView>

      第一个屏幕xml              

<TextView android:id="@+id/textView1" android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_weight="0.12" android:layout_width="match_parent"
    android:text="@string/songsbyyear" android:gravity="center"
    android:textColor="@color/white" android:textSize="22dp"
    android:background="@color/steelblue" android:textStyle="bold"></TextView>
<ListView android:layout_width="wrap_content"
    android:id="@android:id/list"
    android:drawSelectorOnTop="true" android:divider="#FFCC00"    android:dividerHeight="4px"
    android:background="@color/darkslateblue" android:layout_height="456dp">  </ListView>

1 个答案:

答案 0 :(得分:0)

您需要提供颜色的可绘制或十六进制代码作为背景。当你得到这个

Unable to resolve drawable "@color/steelblue" in attribute "background"
Unable to resolve drawable "@color/darkslateblue" in attribute "background"

因此,提供图像,您也可以使用像“#FF00FF”等

这样的值

修改

我在my values文件夹中包含了一个文件my_color.xml,即 res / values / my_color.xml

my_color.xml包括:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="white">#ffffff</color>
  <color name="steelblue">#4863A0</color>
  <color name="darkslateblue">#2B3856</color>
</resources>

这对我有用。现在试试这个。