검색결과 리스트
프로그래밍에 해당되는 글 84건
글
간단한 버튼을 액티비티에 추가하고, 버튼을 누르면 이벤트를 처리하여 새로운 액티비티를 띄우는 어플리케이션을 만들었습니다. 그리고 새로운 액티비티의 버튼은 웹과 전화 화면으로 전환하는 버튼입니다.
* RelativeLayout 속성
RelativeLayout은 child View를 상대적인 위치에 배치할 수 있도록 해주는 ViewGroup이다.
RelativeLayout은 인터페이스를 디자인 하기에 가장 강력한 레이아웃이다.
물론, 여러개의 LinearLayout을 중첩하여 사용한 레이아웃을 하나의 RelativeLayout을 이용하여 배치가 가능하다.
속성 |
설명 |
layout_above |
~의 위에 배치한다. |
layout_ below |
~의 아래에 배치한다. |
layout_ toLeftOf |
~의 오른쪽에 배치한다. |
layout_ alignLeft |
~와 왼쪽 면을 맞춘다. |
layout_ alignRight |
~와 오른쪽 면을 맞춘다. |
layout_ alignTop |
~와 위 쪽 면을 맞춘다. |
layout_ alignbottom |
~와 아래 쪽 면을 맞춘다. |
layout_ alignParentLeft |
true이면 부모와 왼쪽 면을 맞춘다. |
layout_ alignParentRight |
true이면 부모와 오른쪽 면을 맞춘다. |
layout_ alignParentBottom |
true이면 부모와 아래쪽 면을 맞춘다. |
layout_ alignBaseline |
~와 베이스 라인을 맞춘다. |
layout_ alignWithParentIfMissing |
layout_toLeftOf 등의 속성에 대해 대상값이 발견되지 않으면 부모를 대상값으로 사용한다. |
layout_ centerHorizontal |
true이면 부모의 수평 중앙에 배치한다. |
layout_ centerVertica |
true이면 부모의 수직 중앙에 배치한다. |
layout_ centerInParent |
true이면 부모의 부모 수평, 수직 중앙에 배치한다. |
1. MainActivity.java
첫 화면을 부르는 클래스 입니다. setContentView() 에서 activity_main.xml 을 불러서 구성된 화면을 불러냅니다. setOnClickListener() 는 이벤트를 처리하는 메서드인데 클릭 이벤트 발생시 startActivity() 메서드로 새로운 액티비티를 불러냅니다.
MainActivity.java----------------------------------------------------------------------
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button nextButton = (Button)findViewById(R.id.nextButton);
nextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent nextIntent = new Intent(getApplicationContext(), MenuActivity.class);
startActivity(nextIntent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
2. MenuActivity.java
MainActivity가 아닌 새로운 액티비티입니다. 여기서는 웹, 전화화면, 뒤로가기 버튼이 구성되어 있습니다.
MenuActivity.java----------------------------------------------------------------------
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Button webButton = (Button) findViewById(R.id.webButton);
webButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://m.naver.com"));
startActivity(myIntent);
}
});
Button callButton = (Button) findViewById(R.id.callButton);
callButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("tel:/010-4582-1234"));
startActivity(myIntent);
}
});
Button backButton = (Button) findViewById(R.id.backButton);
backButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "back!", extracted())
.show();
finish();
}
private int extracted() {
return 1000;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
3. activity_main.xml
어플리케이션의 첫 화면의 디자인이 구성되어 있는 xml입니다. <Button /> 으로 화면에 버튼을 보이게 할 수 있습니다.
activity_main.xml----------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/next"
/>
</RelativeLayout>
4. activity_menu.xml
새로운 액티비티의 화면을 구성하는 xml 입니다.
activity_menu.xml----------------------------------------------------------------------
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MenuActivity" >
<Button
android:id="@+id/webButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/web"
/>
<Button
android:id="@+id/callButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/webButton"
android:text="@string/call" />
<Button
android:id="@+id/backButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/callButton"
android:text="@string/back" />
</RelativeLayout>
5. AndroidManifest.xml
manifest.xml 입니다. 여기서 새로운 액티비티를 등록해야만 불러올 수 있습니다. <activity android:name="com.example.bluetooth.MenuActivity"> </activity> 부분이 새로운 액티비티 등록 부분입니다.
AndroidManifest.xml----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bluetooth.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.bluetooth.MenuActivity"
></activity>
</application>
</manifest>
6. strings.xml
화면에 쓰여지는 string 들을 관리하는 xml 입니다.
strings.xml----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ButtonTest</string>
<string name="next">Next</string>
<string name="web">Web</string>
<string name="call">Call</string>
<string name="back">Back</string>
<string name="menu_settings">Settings</string>
</resources>
어플리케이션 실행시 첫 화면.
Next 버튼 클릭했을 때 새로운 액티비티.
Web 버튼 클릭시 나타나는 화면. 설정한 naver 페이지로 이동.
Call 버튼 클릭시 나타나는 화면. 설정해 놓은 전화번호가 바로 보인다.
'프로그래밍 > ㆍAndroid' 카테고리의 다른 글
[Android] ADT (Android Virtual Devices) 설치하기 (2) | 2013.05.08 |
---|---|
[Android] BluetoothChat 예제 분석 - 블루투스 장치 켜기 (11) | 2013.01.13 |
[Android] 비트맵, 캔버스, 렉트 등 사용하여 이미지 효과주기 (0) | 2012.02.15 |
[Android] 리스트뷰를 이용한 목록 아이템 출력 - ListView 이용 예제 (2) | 2012.02.12 |
RECENT COMMENT