8 thg 5, 2011

Game Sudoku (Android) -- Tạo Layout cho game

Hnay mình sẽ giới thiệu cho các bạn cách tạo layout cho game sudoku này.
Sau khi hoàn thành chúng ta sẽ có dc kết quả sau :


Trước hết bạn phải hình dung ra rằng game của mình sẽ có layout như thế nào.

ở đây mình tạo layout theo dạng Linear Layout. Các bạn có thể chọn dạng Layout khác tùy ý. Các bạn có thể xem 1 số bài mình đã hướng dẫn tạo layout như :
Trước hết bạn vào trong res/values/colors.xml để tạo background cho game của bạn :
<?xml version="1.0" encoding="utf-8"?>
<resources>
      <color name="backgound">#3500ffff</color>
</resources>
Và trong file res/values/strings.xml bạn viết code như sau :
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Sudoku</string>
    <string name="main_title">Android Sudoku</string>
    <string name="continue_lable">Continue</string>
      <string name="new_game_lable">New Game</string>
      <string name="about_lable">About</string>
      <string name="exit_lable">Exit</string>
</resources>
Đến đây bạn đã tạo dc màu background cho ứng dụng của bạn và các lable cho các button của bạn sẽ làm sau đây.
Tiếp theo bạn tạo layout chính cho chương trình :
Trong ví dụ của mình, mình chia làm 2 Linear layout.1 cái chứa image logo game của ứng dụng, cái còn lại chức các button.
Bạn vào file layout/main.xml
<LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
            <ImageView
                  android:id="@+id/imgSudoku"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:src="@drawable/image"
                   />
      </LinearLayout>  
Logo : ten image của bạn.
Để vẽ các button bạn tạo ra 1 LinearLayout như sau :
<LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center">   
      <Button
            android:id="@+id/continue_button"
            android:text="@string/continue_lable"
            android:layout_width="200px"
            android:layout_height="50px"
            android:lines="1"
            android:layout_gravity="center"
            android:padding="10px"/>
      <Button
            android:id="@+id/new_button"
            android:text="@string/new_game_lable"
            android:layout_width="200px"
            android:layout_height="50px"
            android:lines="1"
            android:layout_gravity="center"
            android:padding="10px"
            />
      <Button
            android:id="@+id/about_button"
            android:text="@string/about_lable"
            android:layout_width="200px"
            android:layout_height="50px"
            android:lines="1"
            android:layout_gravity="center"
            android:padding="10px"
            />
      <Button
            android:text="@string/exit_lable"
            android:id="@+id/exit_button"
            android:layout_width="200px"
            android:layout_height="50px"
            android:lines="1"
            android:layout_gravity="center"
            android:padding="10px"
            />
            <!-- <TextView
            android:text="@string/main_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginBottom="25dip"
            android:textSize="24.5sp" /> -->
      </LinearLayout>  
Các bạn có thể tùy biến các kiểu layout khác nhau cho màn hình game của mình.






1 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. ...