如何在android studio

时间:2017-10-02 08:56:52

标签: java android

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativer);
layout.setBackgroundResource(R.drawable.bgorange);

我希望从微调器中检索bgorange。所以bgorange现在是一个静态值。

1 个答案:

答案 0 :(得分:0)

通常,要以编程方式获取资源,您可以使用:

public static final Drawable getDrawable(Context context, int id) {
    return ContextCompat.getDrawable(context, id);
}

所以,在你的例子中,你可以像这样使用它:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativer);
Drawable drawable = getDrawable(context, YourSpinner.getYourResBackground());
layout.setBackgroundDrawable(drawable)

请注意,您需要build.grade中的最新支持库

compile 'com.android.support:support-v4:xxxxxx