티스토리 뷰

Android/UI

Button

parkho79 2019. 7. 26. 10:59
728x90



 

Button 은 사용자가 클릭하거나 누르는 행동으로 특정 action 을 실행하기 위해 사용한다.

Button 은 크게 text 기반 button 과 image 기반 button 으로 나눌 수 있다.

Image 기반의 button 은 text 와 image 를 모두 포함할 수 있고 ImageButton 이 별도로 존재한다.



How to

◼ Button code in XML

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho" />
view raw layout.xml hosted with ❤ by GitHub

 

◼ Button code in JAVA

Button btn = (Button) findViewById(R.id.simple_btn);
btn.setText("Parkho");
view raw main.java hosted with ❤ by GitHub



Attributes

◼ id

id 는 해당 button 을 유일하게 식별할 수 있는 속성이다.

<Button
android:id="@+id/simple_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
view raw layout.xml hosted with ❤ by GitHub



◼ text

Button 에 표시할 text 를 설정한다.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho" />
view raw layout.xml hosted with ❤ by GitHub

 



◼ gravity

Text 를 정렬할 수 있는 속성이다.

 - layout_width, layout_height 이 wrap_content 이면 text 가 정렬될 공간이 없어 정렬이 안된다.

 - gravity 를 지정하지 않으면 기본 속성인 left, top 이 적용된다.

[Android/Layout] - TextView gravity (TextView gravity 와 동일하다.)

 

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Parkho" />
view raw layout.xml hosted with ❤ by GitHub

 



◼ textColor

Text 의 색상을 설정한다.

- “#argb”, “#rgb”, “#rrggbb”, or “#aarrggbb” 형태로 지정할 수 있다.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho"
android:textColor="#0000FF" />
view raw layout.xml hosted with ❤ by GitHub

 



◼ textSize

Text 의 크기를 설정한다.

- sp(scale independent pixel) or dp(density pixel) 로 설정 할 수 있다.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho"
android:textSize="50.00sp" />
view raw layout.xml hosted with ❤ by GitHub

 



◼ textStyle

Text 의 style 을 설정한다.

- "bold"(굵게), "italic"(이태릭), "normal"(기본) 을 설정할 수 있고 "|" 로 2개 이상의 설정을 할 수 있다.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho"
android:textStyle="bold | italic" />
view raw layout.xml hosted with ❤ by GitHub

 



◼ background

Button 의 배경색을 설정한다.

- color 값이나 drawable 을 설정할 수 있다.

 

◼ padding

Button 의 왼쪽, 오른쪽, 위, 아래 padding 을 설정한다.

 

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho"
android:background="#00FF00"
android:padding="15.00dp" />
view raw layout.xml hosted with ❤ by GitHub

 



◼ drawableBottom, drawableTop, drawableLeft, drawableRight

drawable 객체를 표시할 위치를 지정한다.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parkho"
android:drawableRight="@drawable/ic_launcher" />
view raw layout.xml hosted with ❤ by GitHub

 



728x90

'Android > UI' 카테고리의 다른 글

ImageView  (0) 2019.08.06
ImageButton  (0) 2019.08.05
RadioButton & RadioGroup  (0) 2019.07.22
CheckBox  (0) 2019.07.18
EditText  (0) 2019.07.15
댓글