본문 바로가기

프로그래밍/안드로이드(Android)

[Android] Button 에 text 와 image 넣기 (xml , source)

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Button 의 drawableLeft / drawableRight / drawableTop / drawableBottom (이하 drawableLRTB) 을 이용해 button에 이미지를 넣을 수 있다. 각각 텍스트를 기준으로 왼쪽 / 오른쪽 / 위 / 아래에 이미지가 위치 하게 된다.

아래는 xml에 포함되는 button에 적용된 모습니다.

<Button
    android:id="@+id/btn_favorite"
    android:layout_width="@dimen/_140px"
    android:layout_height="@dimen/_43px"
    android:background="@drawable/album_end_btn_bg"

    android:drawableLeft="@drawable/btn_liketoggle_on_off"
    android:textSize="@dimen/_20pt"
    android:textColor="@color/album_end_btn_color"
    android:singleLine="true"
    android:text="@string/add_fovorite"
    android:layout_marginLeft="@dimen/_4px"
    android:gravity="center"
    android:paddingLeft="@dimen/_15px"
    android:layout_gravity="left|center_vertical"/>

아래는 위의 내용을 화면에 그려준 모습니다.



좀 더 추가로 설명하자면 위 내용은 xml에서의 사용 방법이다.

source 에서는 setdrawableLeft 같은 메소드는 존재 하지 않는다 혹시 위에 것만 읽고 찾아보았다면 낭패볼 듯..

source에서는 setCompoundDrawablesWithIrtrinsicBounds라는 메소드를 사용한다.
아래는 개발 중에 들어가있는 코드의 일부다.

   if (isRequest) {
    favoriteBtn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_liketoggle_on_half, 0, 0, 0);
   } else {
    favoriteBtn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.btn_liketoggle_on, 0, 0, 0);
   }


favoriteBtn은 예상하다시비 button이고 내부 메소드인 setCompoundDrawablesWithIrtrinsicBounds를 사용하였다.
param 값으로 는 순서대로 (Left, Top, Right, Bottom)의 resourceId를 넣어 주면 된다.


저 한줄로 간편히 끝낼 수 있다.
혹 Layout에 textView와 imageView를 사용했다면 빨리 바꿔버리자 소스 길이가 확 줄어들 것이다.


정보 공유는 좋지만 불펌은 반대 합니다.