谁能告诉我android中setBackgroundResource(resourceid)setBackgroundDrawable(getResource().getDrawable(drawableid))有什么区别?

最佳答案

您可以自己看看Android source code for the View class,发现差别很小!

public void setBackgroundResource(int resid) {
    if (resid != 0 && resid == mBackgroundResource) {
        return;
    }

    Drawable d= null;
    if (resid != 0) {
        d = mResources.getDrawable(resid);
    }
    setBackground(d);

    mBackgroundResource = resid;
}

setBackground()只是调用到setBackgroundDrawable() ...
public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

09-12 17:22