태그 보관물: android-layout

android-layout

setCompoundDrawables ()를 호출하면 Compound Drawable이 표시되지 않습니다 setCompoundDrawables메서드를 호출 한 후

setCompoundDrawables메서드를 호출 한 후 , Drawable 복합은 표시되지 않습니다.

Drawable myDrawable = getResources().getDrawable(R.drawable.btn);
btn.setCompoundDrawables(myDrawable, null, null, null);

이견있는 사람?



답변

을 사용해야했습니다 setCompoundDrawablesWithIntrinsicBounds.


답변

이것을 사용하십시오 (테스트했습니다). 잘 작동합니다

Drawable image = context.getResources().getDrawable( R.drawable.ic_action );
int h = image.getIntrinsicHeight(); 
int w = image.getIntrinsicWidth();   
image.setBounds( 0, 0, w, h );
button.setCompoundDrawables( image, null, null, null );


답변

지정된 범위가 없기 때문에 이미지가 비어 있습니다. 사용할 수는 setCompoundDrawables()있지만 Drawable.setBounds()방법을 사용하여 이미지의 경계를 지정하기 전에


답변

상단으로 설정된 예 :

view.setCompoundDrawablesWithIntrinsicBounds(
    null,
    getResources().getDrawable(R.drawable.some_img),
    null,
    null
);

인수 순서 : (왼쪽, 위, 오른쪽, 아래)


답변

다시 조금 더 간단하게 :

Drawable image = context.getResources().getDrawable(R.drawable.ic_action );
image.setBounds( 0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight() );
button.setCompoundDrawables( image, null, null, null );


답변

API 22에서는 더 이상 사용되지 않습니다.

이 코드는 나에게 유용합니다.

Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null);
drawable.setBounds(0, 0, drawable.getMinimumWidth(),
drawable.getMinimumHeight());
tv.setCompoundDrawables(drawable, null, null, null);


답변

코 틀린에서 :

1) 설정 drawable:

val drawable = ContextCompat.getDrawable(context!!,R.drawable.ic_image)?.apply {
    setBounds(0, 0, intrinsicWidth, intrinsicHeight)
}

또는

val drawable = ResourcesCompat.getDrawable(resources, R.drawable.ic_image, null)?.apply {
    setBounds(0, 0, minimumWidth, minimumHeight)
}

2) 설정 TextView:

textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)

또는

button.setCompoundDrawables(null, drawable, null, null)