노력과 삽질 퇴적물

안드로이드: 기초정리(2) 본문

프로그래밍note/언어. JAVA & JDK 계열

안드로이드: 기초정리(2)

MTG 2012. 9. 3. 09:38

Android APIs Reference


안드로이드: 기초정리(1)

안드로이드: 기초정리(2)

안드로이드: 기초정리(3)

안드로이드: 기초정리(4)


추천자료: 안드로이드 개발시 100가지 팁

추천자료: [Android] 정보 정리

추천자료: Android,Easy Tutorials...






04. 레이아웃

 1. 리니어레이아웃

 XML파일

 res/layout

-> orientation에 horizontal/vertical선택가능

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal"  >

   .........

   .........

</LinearLayout>




 2. 릴레티브 레이아웃

 XML파일
 res/layout


* 특정 컴포넌트기준의 상대위치

android:layout_toRightOf="@id/???"

android:layout_toLeftOf="@id/???"

android:layout_above="@id/???"

android:layout_below="@id/???"


* 특정 컴포넌트와 변 맞추기

android:layout_alignLeft="@id/???"

android:layout_alignRight="@id/???"

android:layout_alignTop="@id/???"

android:layout_alignBottom="@id/???"


* 기준점을 잡은 컴포넌트에 추가방향지정

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:layout_alignParentTop="true"

android:layout_alignParentBottom="true"

android:layout_centerInVertical="true"

android:layout_centerInHorizontal="true"

android:layout_centerInParent="true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"  >


    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:text="URL" />

    <EditText

        android:id="@+id/editText1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/textView1"

        android:ems="10"

><requestFocus /></EditText>

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="취소"

        android:layout_below="@id/editText1"

        android:layout_alignParentRight="true"

/>

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="OK"

        android:layout_below="@id/editText1"

        android:layout_toLeftOf="@id/button1"

/>


</RelativeLayout>




 3. 프레임 레이아웃

-> 형태상 모든 레이아웃중 가장 단순.

-> 프레임 레이아웃내에 다른 레이아웃을 차일드로 포함가능.

-> 차일드를 배치하는 규칙이 따로없고, 레이아웃내 모든 차일드는 프레임의 좌측상단에 나타난다.

-> 먼저배치한 차일드(컴포넌트)가 나중에 배치된 차일드 밑에 깔린다.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent" >


    <LinearLayout

... ... ...

... ... ...

  >

        <TextView

... ... ...

... ... ...

/></LinearLayout>


    <LinearLayout

... ... ...

... ... ...

>

        <TextView

... ... ...

... ... ... />

    </LinearLayout>


</FrameLayout>




 4. 테이블 레이아웃

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:stretchColumns="*" >


    <TableRow

        android:id="@+id/tableRow1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

        <Button

            android:id="@+id/button1"

            style="?android:attr/buttonStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="1" />

        <Button

            android:id="@+id/button2"

            style="?android:attr/buttonStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="2" />

        <Button

            android:id="@+id/button3"

            style="?android:attr/buttonStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="3" />

        <Button

            android:id="@+id/buttonb"

            style="?android:attr/buttonStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="4" />

    </TableRow> <!--1행-->


    <TableRow

        android:id="@+id/tableRow2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

        <Button

            android:id="@+id/button12"

            style="?android:attr/buttonStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="위치지정"

            android:layout_column="1" />

        <Button

            android:id="@+id/buttonb"

            style="?android:attr/buttonStyleSmall"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="확장"

            android:layout_span="2" />

    </TableRow> <!--2행-->


</TableLayout>






05. 위젯과 어댑터뷰

 1. 위젯과 이벤트 처리 - 유형1

-> 자바코드에서 이벤트 연결

① res/layout

... ... ...

<Button

        android:id="@+id/eventButton1"

... ... ...

... ... ...

        android:text="이벤트처리 예시1" />

<Button

        android:id="@+id/eventButton2"

... ... ...

... ... ...

        android:text="이벤트처리 예시2" />

... ... ...

② src/패키지

import android.widget.Toast;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;


public class ActivityMain extends Activity implements OnClickListener

{

Button bt1, bt2 ;


public void eventXXX(View v)

{

bt1 = (Button)findViewById(R.id.eventButton1);

bt1.setOnClickListener(this);

}

public void onClick(View v)

{

int buttonID = v.getId();


switch ( buttonID )

{

case R.id.eventButton1:

Toast.makeText(context, "클릭확인", Toast.LENGTH_LONG).show();

break;

}

}


//annonymousclass

        bt2 = (Button)findViewById(R.id.eventbutton2);

        OnClickListener bh2 = new OnClickListener() {

public void onClick(View v2) {

b2 = (Button)v2;

b2.setBackgroundColor(Color.GREEN);

b2.setTextColor(Color.RED);

b2.setText("버튼2 클릭");

}

};

bt2.setOnClickListener(bh2);

}




 2. 위젯과 이벤트 처리 - 유형2

-> 레이아웃에서 이벤트 연결

① res/layout

... ... ...

<Button

        android:id="@+id/clickToEvent"

... ... ...

... ... ...

        android:onClick="eventXXX"

        android:text="이벤트처리 예시2" />

... ... ...

② src/패키지

import android.widget.Toast;


public class ActivityMain extends Activity implements OnClickListener

{

public void eventXXX(View v)

{

int buttonID = v.getId();


switch ( buttonID )

{

case R.id.clickToEvent:

Toast.makeText(context, "클릭확인", Toast.LENGTH_LONG).show();

break;

}

}

}




 3. 리스트뷰

① 각 아이템 항목을 XML에 적의

② ArrayAdapter객체를 생성해서 XML 연결.

③ 리스트뷰_객체의 setAdapter매소드


-> 데이터 그룹에 대한 각각의 정보들을 항목별로 출력

-> ArrayAdapter클래스를 사용하거나 재정의(오버라이드)해서 구현.

-> Adapter인터페이스로 데이터접근

-> 추천자료: http://www.tipssoft.com/bulletin/tb.php/FAQ/918

① res/layout

activity_main.xml



② res/layout

list_child_button.xml

list_child_edittext.xml

list_child_textview.xml

simple_list_child.xml






③ src/패키지


-> 해당 코드에서는res/drawable 폴더내에 car1~6, plane1~4인 이미지 파일이 있어야 한다.(파일포맷무관)






 4. 커스텀 리스트뷰

어댑터 클래스

1. BaseAdapter 를 상속(구현)

2. DataSource갯수를 return

3. Child View객체를 return

4. ItemClick Event 시호출되는메쏘드


-> 추천자료-1. http://croute.me/413

-> 추천자료-2. http://androidhuman.tistory.com/205

① res/layout

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >


    <RelativeLayout

        android:id="@+id/listshow"

        android:layout_width="match_parent"

  ... ... ...

        android:visibility="visible" >

        <TextView

            android:id="@+id/textView1"

    ... ... ...

    ... ... ...

            android:text="&lt;차 리스트>"

        <ListView

            android:id="@+id/listView1"

    ... ... ...

    ... ... ...

            android:layout_below="@+id/textView1">

        </ListView>

    </RelativeLayout>

</FrameLayout>

② res/layout

list_child.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >


    <ImageView

        android:id="@+id/simple_car_imageIV"

        android:scaleType="fitXY"

... ... ...

        android:src="@drawable/car1" />

    <TextView

        android:id="@+id/simple_car_nameTV"

        android:layout_width="fill_parent"

        ... ... ...

... ... ...

        android:text="CARNAME" />


</LinearLayout>

③ src/패키지









06. 인탠트

 1. 이론

인탠트

안드로이드 시스템내 전역메시지

액티비티간 인수와 반환값을 전달

데이터를 모두 주고받는 담당. 왜냐면 공유기억장소가 기본적으로 없어서

안드로이드는 스택에 의한 관리되므로 매소드에 의한 호출X

VM을 넘나든다고 볼수 있다.





 2. 어플리케이션내 액티비티호출


-> 액티비티2개를 이용한 예시

-> 타겟:API Lv.10

① AndroidManifest.xml


-> 어플리케이션에 2개의 액티비티 등록

-> <intent-filter>를 가진 액티비티가 처음 실행시 점유권을 가진다.



② res/layout


-> text에 액티비티 식별을 위한 문자열삽입

-> 레이아웃의 버튼을 통해 다른 액티비티가 호출될수 있게 배치.




③ src/패키지







 3. 어플리케이션간 액티비티 호출


-> 트위터에서 기본카메라앱으로 촬영하는등 별개의 앱끼리 연동되게 하는것.

[참조1]   [참조2]

① AndroidManifest.xml



② res/layout



③ src/패키지


-> intent.setAction("풀 패키지명");으로도 지정 가능.

-> 기본앱이나 다른 앱말고 자신이 만든 다른 앱을 호출했다면, 호출된 액티비티는 finish()로 종료가능.

-> 자신이 만든 다른 앱을 호출하는 방식을 보고 싶다면 아래의 링크 참조.

 [인텐트(Intent) 입문- (1) 액티비티 호출]






 4. 액티비티간 인탠트 인자값 전달


IntentValueDelivery.zip


-> 해당 예시는 액티비티A를 XML로 UI를구현하고 입력값을 액티비티B와 surfaceview(액티비피B소속)로 전달하는 방식이다.

-> 참고자료: http://warmz.tistory.com/77

① res/layout

-> activity_a.xml



② AndroidManifest.xml

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".ActivityA"

            android:label="@string/title_activity_activity" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity

            android:name=".ActivityB"

            android:label="@string/title_activity_activity_b" >

        </activity>

    </application>

③ src/패키지/





'프로그래밍note > 언어. JAVA & JDK 계열' 카테고리의 다른 글

안드로이드: 기초정리(4)  (0) 2012.10.25
안드로이드: 기초정리(3)  (0) 2012.09.17
안드로이드: 기초정리(1)  (0) 2012.08.28
JAVA: 간단한 응용프로그램  (0) 2012.08.28
JAVA: 스레드  (0) 2012.08.27