정보과학 IT

안드로이드 배경화면 바꾸기 (XML과 JAVA의 연동)

물곰탱이 2013. 10. 10. 23:10

[연습예제] 안드로이드 배경화면 바꾸기 (XML과 JAVA의 연동)

 


- 버튼 클릭시 배경화면 컬러가 바뀌는 아주 간단한 예제입니다.
- 안드로이드를 공부하고있는 학생으로서 처음 시작하는 분들에게 xml과 java의 연동을
  이 예제로 조금이나마 이해하셨으면 하는 바램으로 올림..


- BackGroundColorActivity.Java

package Android.BackGroundColor;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.on_clickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class BackGroundColorActivity extends Activity implements on_clickListener{
    /** Called when the activity is first created. */
 Button bt1,bt2,bt3,bt4;  //버튼 생성
 LinearLayout layout;    //레이아웃 생성
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bt1 = (Button)findViewById(R.id.bt_red);  //xml에서 생성한 버튼객체를 가져와 참조
        bt2 = (Button)findViewById(R.id.bt_green); 
        bt3 = (Button)findViewById(R.id.bt_blue); 
        bt4 = (Button)findViewById(R.id.bt_black); 
        bt1.seton_clickListener(this);  //버튼에 Click이벤트 적용
        bt2.seton_clickListener(this);
        bt3.seton_clickListener(this);
        bt4.seton_clickListener(this);
        layout=(LinearLayout)findViewById(R.id.mainlayout);  //xml에서 지정한 Layout객체를 가져와 참조
    }
 @Override

 


//클릭이벤트 시작
 public void on_click(View v) {
  // TODO Auto-generated method stub
  if(v.getId()==R.id.bt_red){  //클릭한 버튼의 아이디가 R.id.bt_red일때
   layout.setBackgroundResource(R.color.red);  //layout의 배경을 R.color.red에서 지정한 컬러로 변경
  }else if(v.getId()==R.id.bt_green){
   layout.setBackgroundResource(R.color.green);
  }else if(v.getId()==R.id.bt_blue){
   layout.setBackgroundResource(R.color.blue);
  }else if(v.getId()==R.id.bt_black){
   layout.setBackgroundResource(R.color.black);
  }  
 }
}


- values/colors.xml (컬러 지정/초기에 이 파일이 없으니 values밑에 colors.xml을 생성해서 코딩해야함)


<?xml version="1.0" encoding="UTF-8"?>
<resources>
<color name="red">#ffff0000</color>
<color name="green">#ff00ff00</color>
<color name="blue">#ff0000ff</color>
<color name="black">#00ffffff</color>
</resources>

-values/strings.xml (레이아웃에 사용할 문자열 지정)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">배경화면 바꾸기 예제</string>
    <string name="app_name">BackGroundColor</string>
    <string name="str_red">빨간색</string>
    <string name="str_green">녹색</string>
    <string name="str_blue">파란색</string>
    <string name="str_black">검은색</string>
</resources>

- layout/main.xml ( LineatLayout 2개로 레이아웃 설정)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/mainlayout"
    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"/>
<ImageView
 android:src="@drawable/icon"
 android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>  
 <LinearLayout
    android:orientation="horizontal"
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <Button
   android:id="@+id/bt_red"
   android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_red"/>
  <Button
   android:id="@+id/bt_green"
   android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_green"/>
  <Button
   android:id="@+id/bt_blue"
   android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_blue"/>
  <Button
   android:id="@+id/bt_black"
   android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_black"/>
 </LinearLayout>
</LinearLayout>

 

http://blog.daum.net/lifeiscool83/24

 

'정보과학 IT' 카테고리의 다른 글

아파치 하둡 (Apache Hadoop)  (0) 2013.10.13
Emulator (에뮬레이터)  (0) 2013.10.13
통합 인증(Single Sign-On, SSO)  (0) 2013.10.10
HTTP 와 HTTPS   (0) 2013.10.10
두가지 구구단으로 익히는 Java   (0) 2013.10.09