3 thg 5, 2011

Bài 1 _Linear Layout

Linear Layout là 1 Viewgroup để hiển thị các phần tử là các View con bên trong theo 1 hướng , hoặc chiều
dọc hoặc chiều ngang.

Bạn có thể tạo 1 project Demo về Linear Layout như sau :
   1. Tạo 1 project với tên là HelloLinearLayout
   2. Mở file main.xml ở thư mục res/layout/main.xml trong project của bạn và thêm đoạn code dưới đây vào.
          <?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:orientation="horizontal"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:layout_weight="1">

            <TextView

                android:text="red"

                android:gravity="center_horizontal"

                android:background="#aa0000"

                android:layout_width="wrap_content"

                android:layout_height="fill_parent"

                android:layout_weight="1"/>

            <TextView

                android:text="green"

                android:gravity="center_horizontal"

                android:background="#00aa00"

                android:layout_width="wrap_content"

                android:layout_height="fill_parent"

                android:layout_weight="1"/>

            <TextView

                android:text="blue"

                android:gravity="center_horizontal"

                android:background="#0000aa"

                android:layout_width="wrap_content"

                android:layout_height="fill_parent"

                android:layout_weight="1"/>

            <TextView

                android:text="yellow"

                android:gravity="center_horizontal"

                android:background="#aaaa00"

                android:layout_width="wrap_content"

                android:layout_height="fill_parent"

                android:layout_weight="1"/>

        </LinearLayout>

        <LinearLayout

            android:orientation="vertical"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:layout_weight="1">

            <TextView

                android:text="row one"

                android:textSize="15pt"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:layout_weight="1"/>

            <TextView

                android:text="row two"

                android:textSize="15pt"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:layout_weight="1"/>

            <TextView

                android:text="row three"

                android:textSize="15pt"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:layout_weight="1"/>

            <TextView

                android:text="row four"

                android:textSize="15pt"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:layout_weight="1"/>

            <TextView

                android:text="row five"

                android:textSize="15pt"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:layout_weight="1"/>

        </LinearLayout>  

    </LinearLayout>               
           
       

Bây giờ bạn mở HelloLinearLayout.java and và thực thi res/layout/main.xml layout in the onCreate() method:
 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

 Bài này mình tham khảo trên dev của android.com.
 Đã test thành công. Rất phù hợp với những bạn mới bắt đầu với Android. Chúc bạn vui vẻ.  
 

Không có nhận xét nào:

Đăng nhận xét

Bản beta đầu tiên

Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...