我有一個在我試圖運行的例子。 有2種方式來做到這一點..Android的TextView
// Get a drawable
ColorDrawble redDrawable = (ColorDrawable).getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackground(redDrawable);
當我把這個在Eclipse IDE中我得到一個錯誤ColorDrawble不能被解析爲一個類型 我在主XML TextView的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:layout_width="fill_parent"
android:id="@+id/texter"
android:layout_height="wrap_content"
android:text="this string"/>
</LinearLayout>
,並在字符串的XML文件中的資源
<resources>
<string name="hello">Hello World, ResourceTesterActivity!</string>
<string name="app_name">ResourceTester</string>
<drawable name="red_rectangle" >#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
<drawable name="green_rectangle" >#f0f0</drawable>
</resources>
**
爲什麼你將可繪製資源存儲在strings.xml中嗎? – Egor
它來自一個adndroid的例子..從PRO android 2 http://www.amazon.com/Pro-Android-Sayed-Y-Hashimi/dp/1430226595 – RBad