반응형
|
안드로이드(Android) XML 을 사용하여 이미지 버튼 배경 로딩하기 |
|
개발환경 : window 7 64bit, Eclipse Kepler, Android 4.2.2 |
|
안드로이드에서 어떤 프로그램의 코딩없이 Main.xml 에 설정해 놓으면 이미지버튼의 배경 그림을 로딩할수 있습니다. |
리소스 파일의 xml 에 이미지를 셋팅해 놓으면
상황별로 로딩되는 이미지가 바뀌게 됩니다.
아래 Xml 을 셋팅한 소스를 보시게 되면
3가지로 분류가 됩니다.
첫번째 포커스가 가고 눌러지지 않았을 때
두번째 포커스가 가고 눌러졌을 때
세번째 포커스가 없고 눌러졌을 때 입니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/androidonfocus"
android:state_focused="true"
android:state_pressed="false"/>
<item android:drawable="@drawable/androidonclick"
android:state_focused="true"
android:state_pressed="true"/>
<item android:drawable="@drawable/androidonclick"
android:state_focused="false"
android:state_pressed="true"/>
<item android:drawable="@drawable/android"/>
</selector>
Activity xml 의 내용입니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
<ImageButton
android:background="@drawable/loadimagebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
메인 Activity 의 소스입니다.
import android.app.Activity;
import android.os.Bundle;
public class SampleActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_activity1);
}
}
아래 그림에서처럼 마우스를 가져간후 클릭하게
되면 그림이 변하게 됩니다. 그리고 포커스를
떠나면 원래 그림대로 돌아옵니다.
반응형
'안드로이드 개발' 카테고리의 다른 글
| 안드로이드(Android) 저장소(Sdcard) 에서 읽어온 파일이 이미지인지 판단하기 (0) | 2014.10.23 |
|---|---|
| 안드로이드(Android) google map API v2 에서 지도에 마커설정하기 (1) | 2014.10.22 |
| 안드로이드(Android) HorizontalScrollView 이용해 이미지 갤러리 만들기 – 2부 (0) | 2014.10.22 |
| 안드로이드(Android) HorizontalScrollView 이용해 이미지 갤러리 만들기 – 1부 (0) | 2014.10.22 |
| 안드로이드(Android) SeekBar 로 배경색 변경하기 (0) | 2014.10.09 |
| 안드로이드(Android) android.util.DisplayMetrics 이용해 해상도를 측정하는 코드 (0) | 2014.09.27 |
| 안드로이드(Android) 미리 정의된 테마를 AndroidManifest.xml 적용하기 (0) | 2014.09.25 |
| 안드로이드(Android) 비트맵의 픽셀값을 읽어와 화면에 그리기 (11) | 2014.09.14 |
