[java] 안드로이드에서 프로그래밍 방식으로 배경 드로어 블을 설정하는 방법

배경을 설정하려면

RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);

가장 좋은 방법입니까?



답변

layout.setBackgroundResource(R.drawable.ready);맞다.
그것을 달성하는 또 다른 방법은 다음을 사용하는 것입니다.

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready) );
} else {
    layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));
}

그러나 큰 이미지를로드하려고하기 때문에 문제가 발생한다고 생각합니다.
다음 은 큰 비트 맵을로드하는 방법에 대한 유용한 자습서입니다.

업데이트 :
API 레벨 22에서 사용되지 않는 getDrawable (int)

getDrawable(int ) 가 이제 API 레벨 22에서 사용되지 않습니다. 대신 지원 라이브러리에서 다음 코드를 사용해야합니다.

ContextCompat.getDrawable(context, R.drawable.ready)

ContextCompat.getDrawable 의 소스 코드를 참조 하면 다음과 같은 내용이 표시됩니다.

/**
 * Return a drawable object associated with a particular resource ID.
 * <p>
 * Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
 * drawable will be styled for the specified Context's theme.
 *
 * @param id The desired resource identifier, as generated by the aapt tool.
 *            This integer encodes the package, type, and resource entry.
 *            The value 0 is an invalid identifier.
 * @return Drawable An object that can be used to draw this resource.
 */
public static final Drawable getDrawable(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 21) {
        return ContextCompatApi21.getDrawable(context, id);
    } else {
        return context.getResources().getDrawable(id);
    }
}

ContextCompat 에 대한 자세한 내용

API 22부터 getDrawable(int, Theme)getDrawable (int) 대신 메소드를 사용해야합니다 .

업데이트 :
지원 v4 라이브러리를 사용하는 경우 다음은 모든 버전에 충분합니다.

ContextCompat.getDrawable(context, R.drawable.ready)

앱 빌드에 다음을 추가해야합니다.

compile 'com.android.support:support-v4:23.0.0' # or any version above

또는 아래와 같은 API에서 ResourceCompat를 사용하십시오.

import android.support.v4.content.res.ResourcesCompat;
ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);


답변

이 시도:

layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));

API 16 <의 경우 :

layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready));


답변

RelativeLayout relativeLayout;  //declare this globally

이제 onCreate, onResume과 같은 함수 안에서

relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.view); //or whatever your image is
setContentView(relativeLayout); //you might be forgetting this


답변

모든 이미지의 배경을 설정할 수도 있습니다.

View v;
Drawable image=(Drawable)getResources().getDrawable(R.drawable.img);
(ImageView)v.setBackground(image);


답변

minSdkVersion 16 및 targetSdkVersion 23을 사용하고 있습니다.
다음은 나를 위해 작동합니다.

ContextCompat.getDrawable(context, R.drawable.drawable);

사용하는 대신:

layout.setBackgroundResource(R.drawable.ready);

오히려 사용 :

layout.setBackground(ContextCompat.getDrawable(this, R.drawable.ready));

getActivity()액티비티 use에서 호출하는 경우 조각에 사용 this됩니다.


답변

배경이 드로어 블 폴더에있는 경우 이미지를 프로젝트의 드로어 블에서 드로어 블-노피 폴더로 이동해보십시오. 이것은 나를 위해 일했습니다. 그렇지 않으면 이미지가 스스로 크기 조정됩니다 ..


답변

butterknife 를 사용 하여 드로어 블 리소스를 클래스 상단에 (메서드 전에) 추가하여 변수에 바인딩 할 수 있습니다.

@Bind(R.id.some_layout)
RelativeLayout layout;
@BindDrawable(R.drawable.some_drawable)
Drawable background;

그런 다음 메소드 중 하나에

layout.setBackground(background);

그게 당신이 필요한 전부입니다