코드로 안드로이드 버튼에 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 답변

  • 좋아요

    0

    싫어요
    채택 취소하기

    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); 이런식으로 쓰시면 됩니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)