我根据android版本编写了一个设置视图背景的函数:
package com.pthuermer.juraquiz;
import android.graphics.drawable.Drawable;
import android.view.View;
public class SupportFunctions {
@SuppressWarnings("deprecation")
public static void setViewBackgroundWithDrawable(View v, Drawable drawable) {
if(android.os.Build.VERSION.SDK_INT < 16) {
v.setBackgroundDrawable(drawable);
}
else {
v.setBackground(drawable);
}
}
}
这就是我所说的:
FrameLayout fl = (FrameLayout)findViewById(R.id.container);
Drawable drawable = getResources().getDrawable(R.drawable.quiz_app_background);
SupportFunctions.setViewBackgroundWithDrawable(fl, drawable);
TextView tv = (TextView)findViewById(R.id.tv_heading_app_launch);
Drawable drw = getResources().getDrawable(R.drawable.quiz_heading_background);
SupportFunctions.setViewBackgroundWithDrawable(tv, drw);
适用于fl
,但不适用于tv
,但会因这些错误而崩溃:
Pick [Android Application]
选择[Android应用程序]
选择[Android应用程序]
选择[Android应用程序]
选择[Android应用程序]
DalvikVM [localhost:8600]
选择[Android应用程序]
DalvikVM [localhost:8600]
线程[&lt; 1&gt; main](Suspended(例外RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread $ ActivityClientRecord,Intent)行:2295
ActivityThread.handleLaunchActivity(ActivityThread $ ActivityClientRecord,Intent)行:2349
ActivityThread.access $ 700(ActivityThread,ActivityThread $ ActivityClientRecord,Intent)line:159
ActivityThread $ H.handleMessage(消息)行:1316
ActivityThread $ H(Handler).dispatchMessage(Message)行:99
Looper.loop()行:137
ActivityThread.main(String [])行:5419
Method.invokeNative(Object,Object [],Class,Class [],Class,int,boolean)line:not available [native method]
Method.invoke(Object,Object ...)行:525
ZygoteInit $ MethodAndArgsCaller.run()行:1187
ZygoteInit.main(String [])行:1003
NativeStart.main(String [])行:不可用[native method]
线程[&lt; 10&gt; Binder_2](跑步)
线程[&lt; 9&gt; Binder_1](正在运行)