안규 공부방

Android > 버튼(Button), 이미지 버튼(ImageButton) 이란 무엇인가?

등록일 : 2017-07-06 17:12 조회수 : 93,874

안드로이드의 유저 인터페이스 중 사용자의 선택 확인 등에 사용되는 요소입니다.

안드로이드 버튼 (Android Button) 상속 계층도
 

		java.lang.Object
		└─ android.view.View
			└─android.widget.TextView
				└─ android.widget.Button
	


버튼의 하위 클래스들 : CompoundButton, CheckBox, Chronometer, DigitalClock, EditText

Button은 사용자가 누를수 있는 형태의 위젯입니다. 누르고 있거나(press), 클릭하거나 등의 행동을 할수 있는 녀석이죠.

1. xml로 Button 만들기

TestButtonActivity.java

		public class TestButtonActivity extends Activity {
			/** Called when the activity is first created. */
			@Override
			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
				setContentView(R.layout.main);

				Button btn01 = (Button)findViewById(R.id.button1);
			}
		}
		


activity_main.xml

		<?xml version="1.0" encoding="utf-8"?>
		<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:orientation="vertical" >
			
			<Button
				android:id="@+id/button1"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:text="버튼1번" 
				android:textColor="#FF0000"/>
		</LinearLayout>
		


만드는 방법은 위처럼 간단합니다.

2. 자바코드에서 안드로이드 버튼 만들기

아래 소스는 자바소스상에서 버튼을 만들어 속성을 적용하는 방법입니다.

TestButtonActivity.java

		public class TestButtonActivity extends Activity {
			/** Called when the activity is first created. */

			@Override
			public void onCreate(Bundle savedInstanceState) {
				super.onCreate(savedInstanceState);
				setContentView(R.layout.main);

				Button btn02 = new Button(this);
				btn02.setText("자바코드로 버튼 만들기");
				btn02.setTextColor(0xFFFF00);
				btn02.setTextSize(30);
			}
		}
		


new Button() 객체를 생성해서 만들어 줄 수 있습니다.

3. 이미지 버튼 예제(XML)

이미지 버튼의 이미지를 설정하는 것은 “android:src” 속성을 이용해야 합니다.
 

		<ImageButton
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:src="@drawable/button_icon"/>
		


4. 일반 Button 에 이미지 추가(XML)

Button 도 TextView 처럼 상하좌우에 이미지 하나를 삽입할 수 있습니다.
 

		<Button
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/button_text"
			android:drawableLeft="@drawable/button_icon" />


 
※ 혹시 도움이 되셨다면 댓글에 한마디 남겨주세요!
작성자   비밀번호
자동글 방지     (자동글 방지 기능입니다.)
내용   댓글달기 
이메일 문의 : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.