[Android] Event Android 2011. 12. 15. 14:38
이벤트는 어떤 특정한 사건이 발생 할 경우 일어나는 것을 말하고 있따.

콜백 메서드를 이용하거나 이벤트 리스너를 이용 할 수 있다.

콜백 메서드는 시스템에 의해 자동적으로 호출되는 메서드이고, 리스너는 이벤트에 대한 인터페이스 임을 알아두자.

콜백의 경우 다음과 같은 간단한 코드로 테스트 할 수가 있다.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView view = new mView(this);
        setContentView(view);
    }
 
 protected class mView extends View
 {
  public mView(Context context){
   super(context);
  }
  
  public boolean onTouchEvent(MotionEvent event){
   super.onTouchEvent(event);
   if(event.getAction()==MotionEvent.ACTION_DOWN){
    Toast.makeText(ImageViewActivity.this, "touch", Toast.LENGTH_SHORT).show();
    return true;
   }
   return false;
  }
 }

이 코드를 실행해서 아무 곳이나 누르면 토스트가 떠서 작동 됨을 확인 할 수 있다.

다음은 리스너를 이용하여 만든 코드를 보자.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = new View(this);
        view.setOnTouchListener(listen);
        setContentView(view);
    }
 
 class listener implements View.OnTouchListener
 { 
  public boolean onTouch(View v, MotionEvent event){   
   if(event.getAction()==MotionEvent.ACTION_DOWN){
    Toast.makeText(ImageViewActivity.this, "touch", Toast.LENGTH_SHORT).show();
    return true;
   }
   return false;
  }
 }
 
 listener listen = new listener();

처음 코드와 다른 점은 View를 생성해서 거기에 해당하는 리스너를 연결 해 준다는 것이다.

또한 인자 값에 View 객체를 받는 인자도 추가 된다. 별 다른 점은 크게 없는 것을 볼 수 있다.

다른 케이스로 액티비티가 리스너를 구현 할 수 있다. 그 방법은 다음과 같다.

public class ImageViewActivity extends Activity implements View.OnTouchListener{
 
    /** Called when the activity is first created. */
 
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = new View(this);
        view.setOnTouchListener(this);
        setContentView(view);
    }
 
 public boolean onTouch(View v, MotionEvent event){   
  if(event.getAction()==MotionEvent.ACTION_DOWN){
   Toast.makeText(ImageViewActivity.this, "touch", Toast.LENGTH_SHORT).show();
   return true;
  }
  return false;
 }
}

다음은 뷰에서 리스너 구현 방법이다.

public class ImageViewActivity extends Activity{
 
    /** Called when the activity is first created. */
 
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView view = new mView(this);
        view.setOnTouchListener(view);
        setContentView(view);
    }
 
 protected class mView extends View implements View.OnTouchListener
  {
   public mView(Context context){
    super(context);
   }
  
   public boolean onTouch(View v, MotionEvent event){
    super.onTouchEvent(event);
    if(event.getAction()==MotionEvent.ACTION_DOWN){
     Toast.makeText(ImageViewActivity.this, "touch", Toast.LENGTH_SHORT).show();
     return true;
    }
    return false;
   }
  }
}

다음은 이너 클래스를 쓰는 방법이다.

public class ImageViewActivity extends Activity{
 
    /** Called when the activity is first created. */
 
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = new View(this);
        view.setOnTouchListener(listener);
        setContentView(view);
    }
   private View.OnTouchListener listener = new View.OnTouchListener() {  
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      if(event.getAction()==MotionEvent.ACTION_DOWN){
       Toast.makeText(ImageViewActivity.this, "touch", Toast.LENGTH_SHORT).show();
       return true;
      }
      return false;
  }
 };
}

마지막으로 가장 함축된 형태를 나타내는 코드로 이벤트를 설정 할 때 그 순간 객체를 생성해서 넘기는 방법이다.

또한 이런 이벤트 핸들러들은 우선 순위를 가진다.

제일 처음 뷰의 리스너이고 다음이 뷰의 콜백 메서드, 마지막으로 액티비티의 콜백 메서드 순으로 호출이 된다.

[Android] MediaPlayer Android 2011. 12. 13. 17:44

안드로이드에서 음원을 재생하는 클래스이다.

먼저 android.Media 를 import 시키고 res 아래에 raw 를 생성한 뒤 그곳에 음원 파일을 import 한다.

단 안드로이드에서는 대문자를 지원하지 않음으로 전부 소문자 파일이어야 한다.

그렇게 준비하고 다음과 같은 코드를 실행 하면 음원이 재생 되는 것을 확인 할 수 있다.

(ain.xml 파일에는 버튼 4개를 준비한다.)

package imageview.test;

import android.app.Activity;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.media.*;
import android.content.*;

public class ImageViewActivity extends Activity {
 Beeper beeper1;
 Beeper beeper2;
    /** Called when the activity is first created. */
 
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        beeper1 = new Beeper(this,R.raw.aid);
        beeper2 = new Beeper(this,R.raw.kalimba);
       
        findViewById(R.id.btn1).setOnClickListener(listener);
        findViewById(R.id.btn2).setOnClickListener(listener);
        findViewById(R.id.btn3).setOnClickListener(listener);
        findViewById(R.id.btn4).setOnClickListener(listener);
    }
 
 Button.OnClickListener listener = new Button.OnClickListener(){
  public void onClick(View v)
  {
   MediaPlayer player;
   switch(v.getId())
   {
    case R.id.btn1 :
     player = MediaPlayer.create(ImageViewActivity.this, R.raw.aid);
     player.start();
     break;
    case R.id.btn2 :
     player = MediaPlayer.create(ImageViewActivity.this, R.raw.kalimba);
     break;
    case R.id.btn3 :
     beeper1.play();
     break;
    case R.id.btn4 :
     beeper2.play();
     break;
   }
  }
 };
 
 class Beeper
 {
  MediaPlayer player;
  Beeper(Context context, int id)
  {
   player = MediaPlayer.create(context, id);
  }
  
  void play()
  {
   player.seekTo(0);
   player.start();
  }
 }
}

위 코드에서 1번과 2번의 경우 무작위로 누를 경우 프로그램의 부하가 생기지만 3번과 4번의 경우는 거의 생기지 않는다. 반응과 안정성에서는 훨씬 좋다.

다음은 SoundPool을 이용한 방법이다.

SoundPool pool;
 int flag=0;
    /** Called when the activity is first created. */
 
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        pool = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
        flag = pool.load(this,R.raw.aid,1);
        findViewById(R.id.btn1).setOnClickListener(listener);
        findViewById(R.id.btn2).setOnClickListener(listener);
        findViewById(R.id.btn3).setOnClickListener(listener);
        findViewById(R.id.btn4).setOnClickListener(listener);
    }
 
 Button.OnClickListener listener = new Button.OnClickListener(){
  public void onClick(View v)
  {
   switch(v.getId())
   {
    case R.id.btn1 :
     pool.play(flag, 1, 1, 0, 0, 1);     
    case R.id.btn2 :
     pool.play(flag, 0.5f, 0.5f, 0, 0, 1);
    case R.id.btn3 :
     pool.play(flag, 1, 1, 0, 2, 1);
    case R.id.btn4 :
     pool.play(flag, 1, 1, 0, 0, 0.5f);
   }
  }
 };

얼핏봐도 처음 코드 보단 단순하다. SoundPool의 생성자의 첫번재 인수는 스트림갯수이고, 그 다음은 타입, 그 다음은 품질에 해당하는 것이다.

play 메서드를 보면 재생할 사운드 지정하고, 그 다음이 좌측 볼륨, 우측 볼륨, 재생 우선 순위, 반복 횟수, 재생 속도를 나타낸다)

재생 반복 횟수에서 0은 한번만 재생하는 것이다.

[Android] Toast Android 2011. 12. 13. 14:01

토스트는 안드로이드의 시스템 차원에서 제공하는 팝업이다.

뭐.. 볼륨이 조절 되었다던가.. 메세지가 정상 처리 되었다거나.. 등등의 경우에 이용되고, 디버깅 용으로도 종종 이용이 된다.

이것들은 특정 시간이 지나면 자동으로 사라지며 사용자 작업에 대해서 방해 하지 않는다.

사용방법은 다음과 같다.

먼저 3개의 버튼을 만들고

<Button
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/btn1"
     android:text="1번"
 />
 <Button
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/btn2"
     android:text="2번"
 />
 <Button
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/btn3"
     android:text="3번"
 />
java 파일 상에서 다음과 같은 소스 코드를 넣는다.

package imageview.test;

import android.app.Activity;
import android.os.*;
import android.view.*;
import android.widget.*;

public class ImageViewActivity extends Activity {
    /** Called when the activity is first created. */
 
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.btn1).setOnClickListener(listener);
        findViewById(R.id.btn2).setOnClickListener(listener);
        findViewById(R.id.btn3).setOnClickListener(listener);
    }
 
 Button.OnClickListener listener = new Button.OnClickListener(){
  public void onClick(View v)
  {
   switch(v.getId())
   {
    case R.id.btn1 :
     Toast.makeText(ImageViewActivity.this, "잠깐 뿅~", Toast.LENGTH_SHORT).show();
     break;
    case R.id.btn2 :
     Toast.makeText(ImageViewActivity.this, "오래 뿅~", Toast.LENGTH_LONG).show();
     break;
    case R.id.btn3 :
     LinearLayout linear = (LinearLayout)View.inflate(ImageViewActivity.this, R.layout.main, null);
     Toast toast = new Toast(ImageViewActivity.this);
     toast.setView(linear);
     toast.show();
     break;
   }
  }
 };
}
각기 버튼을 눌러 보면 약 2초, 4초 후에 사라지며,

맨 마지막 것은 현재 화면에 나온 것들을 Toast로 보여준다. 물론 이 역시 약간의 시간이 지나면 사라진다.

[Android] Canvas Android 2011. 12. 13. 13:41

캔버스는 하나의 GUI 객체들을 표시 할 수 있는 화면이다.

다음은 간단한 캔버스 이용 방법이다.

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.*;
import android.graphics.*;

public class ImageViewActivity extends Activity {
    /** Called when the activity is first created. */
 View page1, page2, page3;
 @Override   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView view = new mView(this);
        setContentView(view);
    }
 
 protected class mView extends View
 {
  public mView(Context context)
  {
   super(context);
  }
  
  public void onDraw(Canvas canvas)
  {
   Paint pnt = new Paint();
   pnt.setColor(Color.BLACK);
   //canvas.drawColor(Color.YELLOW);
   //canvas.drawCircle(100,100,80,pnt);
   
   canvas.drawPaint(pnt);
   RectF rect = new RectF(10,10,100,100);   
   pnt.setColor(Color.WHITE);   
   canvas.drawRoundRect(rect,10,10, pnt);
   
   //pnt.setColor(Color.WHITE);
   //canvas.drawOval(new RectF(10,70,100,120), pnt);
   //canvas.drawText("Text", 110, 40, pnt);
   //pnt.setAntiAlias(true);
   //canvas.drawOval(new RectF(10,70,100,120), pnt);
   //canvas.drawText("Text", 110, 100, pnt);
  }
 }
}

위에서 보면 setAntiAlias 가 보이는데 이것은 글자를 표현 할 때 부드럽게 보이게 하는 효과를 나타낸다.

인자 값은 boolean 형태로 조절하여 쓸 수 있다.

중첩 레이아웃은 레이아웃들의 조합을 말하고 있다.

레이아웃 자체가 View 클래스에서 파생 됨으로 이들은 얼마든 중첩되게 이용하여도 된다.

간단하게 살펴보면 아래 샘플 코드를 보면 된다.

<?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"
     >
   
    <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="상위레이아웃"   
    />
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     >
  <TableRow>
     <TextView android:text ="1" />
     <TextView android:text ="2" />
     <TextView android:text ="3" />
 </TableRow>
 <TableRow>
     <TextView android:text ="3" />
     <TextView android:text ="2" />
     <TextView android:text ="1" />
 </TableRow>               
    </TableLayout>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
     >
 <TextView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="하위레이아웃"
 />
 </LinearLayout>
</LinearLayout>

여기에서 살펴보면 먼저 라이너 레이아웃이 먼저 붙고 그 아래 테이블 레이아웃 그리고 마지막으로 다시 라이너 레이아웃이 붙는다. 실행해보면 특성에 맞게 볼 수가 있다.

그 다음으로 페이지 전환 형식으로 구성해보면 다음과 같다

<?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"
     >
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
     >
     <Button
      android:id="@+id/btn1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="1번"   
     />
     <Button
      android:id="@+id/btn2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="2번"   
     />
     <Button
      android:id="@+id/btn3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="3번"   
     />
    </LinearLayout>   
     <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
      >
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:id="@+id/page1"
      >
    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="1번 페이지"  
    />          
      </LinearLayout>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:visibility="invisible"
          android:id="@+id/page2"
         >
          <EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/t_edit"
              android:text="2번 페이지"             
          />
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignRight="@id/t_edit"
              android:text="버튼"
          />
      </RelativeLayout>
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:visibility="invisible"
          android:id="@+id/page3"
         >
          <TableRow> android:text ="1" </TableRow>
          <TableRow> android:text ="2" </TableRow>
          <TableRow> android:text ="3" </TableRow>
      </TableLayout>
     </FrameLayout>
</LinearLayout>

main.xml 내용은 다음과 같이 하고 java 파일은 아래와 같이 입력하고 동작을 시켜보면 버튼을 누를 때 마다 다른 화면 처럼 지정한 레이아웃들이 보일 것이다.

<?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"
     >
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
     >
     <Button
      android:id="@+id/btn1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="1번"   
     />
     <Button
      android:id="@+id/btn2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="2번"   
     />
     <Button
      android:id="@+id/btn3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="3번"   
     />
    </LinearLayout>   
     <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
      >
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:id="@+id/page1"
      >
    <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="1번 페이지"  
    />          
      </LinearLayout>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:visibility="invisible"
          android:id="@+id/page2"
         >
          <EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/t_edit"
              android:text="2번 페이지"             
          />
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignRight="@id/t_edit"
              android:text="버튼"
          />
      </RelativeLayout>
      <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:visibility="invisible"
          android:id="@+id/page3"
         >
          <TableRow>
           <TextView android:text ="1" />
           <TextView android:text ="2" />
          </TableRow>
          <TableRow>
              <TextView android:text ="2" />
           <TextView android:text ="1" />
          </TableRow>
      </TableLayout>
     </FrameLayout>
</LinearLayout>
[Android] TableLayout Android 2011. 12. 12. 18:03
테이블 레이아웃은 일종의 표를 생각하면 된다.

열과 행으로 이루어지는 형식이며 이 높이는 wrap_content에 귀속된다.

다음 예제를 실행해보면 java의 JTable 형태 처럼 나타나는 것을 확인 할 수 있다.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
   
 <TableRow>
     <TextView android:text ="1" />
     <TextView android:text ="2" />
     <TextView android:text ="3" />
 </TableRow>
 <TableRow>
     <TextView android:text ="3" />
     <TextView android:text ="2" />
     <TextView android:text ="1" />
 </TableRow>
</TableLayout>

또한 부모 폭에 맞추게 할려면 shrinkColumns 이용하여 축소 하거나 stretchColumns 이용하여 확장 시킬 수 있다.
[Android] FrameLayout Android 2011. 12. 12. 17:55

이녀석은 더욱 단순하다. 그냥 프레임들이 겹쳐 있다.

처음에 붙인 컴포넌트 위에 또 붙고 붙고 그런 형식이다.

이런 내용 같은 경우 어떤 컴포넌트를 visibility 속성을 이용하여 숨겼다 보였다 하기에 쓸만하다.

View Group의 일종으로 addView, removeView 를 이용하여 추가 삭제가 가능하다.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
   
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn1"
     android:text="first"
 />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn2"
  android:text="second"
 />
</FrameLayout>

아래 코드를 추가해서 보면 서로 겹쳐진 녀석들을 볼 수가 있다.

[Android] AbsoluteLayout Android 2011. 12. 12. 17:41
앱솔루트레이아웃.. 그냥 말 그대로 원하는 좌펴에 찍어주는 것이다. 좌표는 layout_x, layout_y 를 이용하여 픽셀 계산한다.

다음은 한번 보고 갈 예제이다.

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >
   
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn1"
     android:text="first"
     android:layout_x="50px"
     android:layout_y="50px"
 />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn2"
  android:text="second"
  android:layout_x="150px"
     android:layout_y="150px"
 />
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn3"
     android:text="third"
     android:layout_x="250px"
     android:layout_y="250px"
 />
</AbsoluteLayout>
[Android] RelativeLayout Android 2011. 12. 12. 17:35
관계형 레이아웃이다. 모두 접두어에 layout_ 이 붙어 있고 다음이 그 관련된 설명이다

 속성  설명
 layout_above  ~의 위에
 layout_below  ~의 아래에
 layout_toLeftOf  ~의 왼쪽에
 layout_toRightOf  ~의 오른쪽에
 layout_alignLeft  ~의 왼쪽 변을 맞춘다
 layout_alignTop  ~의 위쪽 변을 맞춘다
 layout_alignRight   ~의 오른쪽 변을 맞춘다
 layout_alignBottom  ~의 아래쪽 변을 맞춘다
 layout_alignParentLeft  true 면 부모와 왼쪽 변 맞춘다
 layout_alignParentTop   true 면 부모와 위쪽 변 맞춘다 
 layout_alignParentRight  true 면 부모와 오른쪽 변 맞춘다
 layout_alignParentBottom  true 면 부모와 아래쪽 변 맞춘다
 layout_alignBaseline  ~와 베이스라인 맞춘다
 layout_alignWithParantIfMissing  속성에 앵커가 발생하지 않을 경우 부모를 앵커로 인지한다
 layout_aligncenterHorizontal  true 면 부모의 수평 중앙에
 layout_aligncenterVertical  true 면 부모의 수직 중앙에 
 layout_aligncenterInParent  true 면 부모의 수평 중앙에 

샘플은 다음과 같이 해서 확인해보면 알 수 있을 것이다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     >    
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn1"
     android:text="first"
     android:layout_marginRight="15px"
 />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/btn2"
  android:text="second"
  android:layout_toRightOf="@id/btn1"
 />
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn3"
     android:text="third"
     android:layout_below="@id/btn2"
 />
</RelativeLayout>
[Android] LinearLayout Android 2011. 12. 12. 17:04
Java에서 FlowLayout과 동일한 형태이다. 단 좀 다른게 있다면, 안드로이드는 가로 세로 형태를 지정 할 수가 있다.

수정 방법은 maili.xml 에서

android:orientation="horizontal/vertical/wrap_content"  속성 중 하나 골라서 그 내용을 확인하면 된다.

아래에 다음과 같은 코드를 넣고 속성 값만 바꿔보자.

<Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn1"
     android:text="first"
     />
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn2"
     android:text="second"
     />
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:id="@+id/btn3"
     android:text="third"
     />

horizontal의 경우 나란히 들어 갈 것이고

vertical 경우 세로로 나란히, wrap_caontent 경우에는 각기 가진 폭만큼 표시가 될 것이다.

또한 gravity를 이용하여 정렬도 가능하다. 기본적으로 left, right, top, bottom, fill, center 이 있고 여기에 fill_ 또는 center_ 를 붙여서 적용해보면 그 결과를 확인 할 수 있을 것이다.

또한 | 를 이용하여 두개의 속성을 같이 쓸 수가 있다.

android:gravity="center_vertical|right" 형식으로 쓰면 된다.

또한 layout_gravity 라는 것이 있는데 이것은 뷰 자체를 부모의 어디에 둘 것인가를 결정한다.

또한 baselineAligned 는 디폴트로 true의 값을 가진다. 이것을 각기 다르게 값을 넣어서 한다면 텍스테 대한 베이스 정렬에 대해 확인 할 수가 있다.

android:layout_weight 속성 값은 부모 레이아웃에 대해 자식 레이아웃끼리 비율을 정해서 차지 할 수 있다.

위 코드에서 android:layout_weight="1/2/3"을 각 부분에 넣고 확인하면 20:60:20 의 비율로 각기 공간을 차지 하고 있음을 확인 가능하다. orientation 속성은 vertical로 하고 확인해야 한다.

또한 마진과 패딩이라는 것이 있다. 마진의 경우 뷰와 부모사이, 패딩의 경우 뷰와 내용물 사이의 간격을 조절한다.

즉, 안쪽과 바깥쪽 여백이라는 것이다.

android:layout_margin = '10px" 또는 android:layout_padding="10px" 를 적용시켜서 테스트 해 보면 결과를 알 수가 있다.