2011. 8. 22. 15:13

안드로이드 Activity 디자인, XML 방식과 Java 방식


1. 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"
>
 <LinearLayout
  android:id="@+id/linearLayout1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"
 >
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/btn1"
   android:text="B1"
  ></Button>
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/btn2"
   android:text="B2"
  ></Button>
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/btn3"
   android:text="B3"
  ></Button>
  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/btn4"
   android:text="B4"
  ></Button>
 </LinearLayout>
 <LinearLayout
  android:id="@+id/linearLayout2"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:layout_weight="1"
 >
  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/textView1"
   android:text="RED"
   android:background="#FF0000"
   android:textSize="12pt"
  ></TextView>
  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/textView2"
   android:text="GREEN"
   android:background="#00FF00"
   android:textSize="12pt"
  ></TextView>
  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/textView3"
   android:text="BLUE"
   android:background="#0000FF"
   android:textSize="12pt"
  ></TextView>
  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:id="@+id/textView4"
   android:text="BLACK"
   android:textSize="12pt"
  ></TextView>
 </LinearLayout>
</LinearLayout>

2. Java 소스 방식

package kr.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class LinearLayoutTest2Activity extends Activity { 
 private Button btn1, btn2, btn3, btn4 ;
 private TextView text1, text2, text3, text4 ; 
 
    /-* Called when the activity is first created. *-
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        
        btn1 = (Button)findViewById(R.id.btn1) ;
        btn2 = (Button)findViewById(R.id.btn2) ;
        btn3 = (Button)findViewById(R.id.btn3) ;
        
        text1 = (TextView)findViewById(R.id.textView1);
        text2 = (TextView)findViewById(R.id.textView2);
        text3 = (TextView)findViewById(R.id.textView3);
        text4 = (TextView)findViewById(R.id.textView4);
        
        text1.setTextSize(20) ;
        text1.setText("RED") ;
        text2.setTextSize(20) ;
        text2.setText("GREEN") ;
        text3.setTextSize(20) ;
        text3.setText("BLUE") ;
        text4.setTextSize(20) ;
        text4.setText("BLACK") ;
        
        text1.setBackgroundColor(Color.RED) ;
        text2.setBackgroundColor(Color.GREEN) ;
        text3.setBackgroundColor(Color.BLUE) ;
    }
}
두 경우 모두 같은 결과