코드로 안드로이드 버튼에 drawableLeft를 어떻게 설정하죠?
조회수 10461회
제가 동적으로 버튼을 만드는데요. 먼저 밑에처럼 xml로 스타일을 설정했는데 이걸 코드로 옮기고 싶어요.
<Button
android:id="@+id/buttonIdDoesntMatter"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="buttonName"
android:drawableLeft="@drawable/imageWillChange"
android:onClick="listener"
android:layout_width="fill_parent">
</Button>
이게 제가 원하는건데 밑에처럼 코드로하려니까 쉽지않네요.
linear = (LinearLayout) findViewById(R.id.LinearView);
Button button = new Button(this);
button.setText("Button");
button.setOnClickListener(listener);
button.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
)
);
linear.addView(button);
1 답변
-
setCompoundDrawable 메소드를 쓰면돼요.
1.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley ); img.setBounds( 0, 0, 60, 60 ); txtVw.setCompoundDrawables( img, null, null, null );
2.
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley ); txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);
3.
txtVw.setCompoundDrawablesWithIntrinsicBounds( R.drawable.smiley, 0, 0, 0);
이런식으로 쓰시면 됩니다.
댓글 입력