IT박스

setBackgroundDrawable () 지원 중단됨

itboxs 2020. 11. 1. 17:27

setBackgroundDrawable () 지원 중단됨


그래서 내 sdk는 15에서 21로 이동하고를 호출 setBackgroundDrawable()하면 Android Studio에서 더 이상 사용되지 않는다고 알려줍니다.

나는 그것을 사용하여 돌아 다니는 것을 생각했다.

int sdk = android.os.Build.VERSION.SDK_INT;

if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm));
} else {
    layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));
}

하지만 "setBackground ()"에서 오류가 발생합니다.

그래서 어떻게 처리 하시겠습니까?


흥미로운 주제입니다. 당신이하는 방식은 분명히 정확합니다. 실제로 이름 지정 결정 변경입니다. 이 답변이 지적 했듯이 다음을setBackground() 호출하십시오 setBackgroundDrawable().

public void setBackground(Drawable background) {
    //noinspection deprecation
    setBackgroundDrawable(background);
}

@Deprecated
public void setBackgroundDrawable(Drawable background) { ... }

당신이 볼 수있는 이 스레드를 모든이의에 대한 자세한 내용은.


다음을 시도해 볼 수 있습니다.

setBackgroundResource(R.drawable.img_wstat_tstorm);

이 메서드가 더 이상 사용되지 않기 때문에 재미 있지만 Android 소스 코드를 보면 다음과 같은 것을 찾을 수 있습니다.

   /**
     * Set the background to a given Drawable, or remove the background. If the
     * background has padding, this View's padding is set to the background's
     * padding. However, when a background is removed, this View's padding isn't
     * touched. If setting the padding is desired, please use
     * {@link #setPadding(int, int, int, int)}.
     *
     * @param background The Drawable to use as the background, or null to remove the
     *        background
     */
    public void setBackground(Drawable background) {
        //noinspection deprecation
        setBackgroundDrawable(background);
    }

2018 년 8 월 15 일 기준 수정

지원 라이브러리 사용

Drawable drawable = ResourcesCompat.getDrawable(getResources(), drawableRes, null);
ViewCompat.setBackground(layout, drawable);

getResources (). getDrawable ()은 드로어 블이 아닌 ID (int)를 인수로 사용하므로 오류가 발생합니다. 이 시도:

layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));


제 경우에는 맞습니다.이 문제를 해결하십시오.

 imageView.setBackgroundResource(images[productItem.getPosition()]);

//Java
view.setBackground(ActivityCompat.getDrawable(context, R.drawable.bg))

//Kotlin 
view.background = ActivityCompat.getDrawable(context, R.drawable.bg)

minSdkVersion 16 및 targetSdkVersion 23을 사용하고 있습니다. 다음은 저에게 효과적이며 ContextCompat.getDrawable (context, R.drawable.drawable);

사용하는 대신: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

오히려 사용 :

layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));

getActivity() 활동에서 호출하는 경우 조각에서 사용됩니다. this


2018 년 11 월 23 일 기준 수정

Kotlin :

view.background = resources.getDrawable(R.drawable.ic_image,theme)

테마 매개 변수를 포함하는 경우.


BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem));
        getSupportActionBar().setBackgroundDrawable(background);

참고 URL : https://stackoverflow.com/questions/27141279/setbackgrounddrawable-deprecated