วันนี้จะมาดูรูปแบบของ scaleType ใน ImageView ครับ โดย attribute อันนี้ทำให้เราสามารถกำหนดการรูปแบบการ scale รูปภาพได้ครับ ทำไมต้อง scale ? คงจะมีสักครั้งที่เรามีพื้นที่จำกัด แต่รูปภาพที่จะแสดงนั้นมีหลากหลายขนาดซึ่งบางทีเราอาจจะอยากให้แสดงรูปในอัตราส่วนปกติมากกว่าที่จะบีบรูปลงมา หรือ อาจจะต้องการกำหนดการ scale แบบอื่น
Home
tutorial
แสดงบทความที่มีป้ายกำกับ tutorial แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ tutorial แสดงบทความทั้งหมด
หลังจากจัดการกับปัญหา Hard Disk ตัวเองแล้วก็คิดอยู่นานว่าจะเขียนเรื่องอะไรดี อยากจะเขียนหลายๆเรื่อง แต่คิดว่าคงยากที่จะอธิบาย ยิ่งอธิบายน่าจะยิ่งงง เลยมาตกที่ Custom View หรือคือการสร้าง View ของตัวเองขึ้นมาโดยอาจจะสร้างเป็น library เพื่อเก็บไว้ใช้ภายหลัง หรือว่าอยากได้ view ที่ไม่มีอยู่ใน view มาตรฐาน ในตัวอย่างนี้จะมาลองทำ Number Picker กัน โดยเจ้า Number Picker นี้มีอยู่ใน api level 11 (android 3) ขึ้นไป ในตอนที่ผมเริ่มเขียนมีเครื่อง test เป็น android 2.2 ต้องหาโหลด widget ของที่อื่นมาใช้ ซึ่งบางทีมันก็ไม่ได้ตรงกับที่เราต้องการเท่าไหร่นัก มาลองกันเลยครับ หน้าตาอาจจะธรรมดาไปหน่อยนะครับ
หลังจากตัวอย่างในตัวอย่างที่แล้ว ที่เราสร้าง ListActivity โดยแต่ละรายการมีรูปที่แตกต่างกันโดยใช้ SimpleAdapter วันนี้เราจะมาใช้ Adapter อีกตัวหนึ่งคือ BaseAdapter โดยวิธีการนี้เราต้องสร้าง class ขึ้นมาโดยสืบทอด BaseAdapter วิธีนี้เราสามารถควบคุมการแสดงผลได้มากกว่าที่ผ่านมา ตัวอย่างที่เราจะทำวันนี้ดัดแปลงมาจากตัวอย่างที่แล้ว โดยในแต่ละรายการจะมีจำนวน และให้ highlight แถวที่มีจำนวนน้อยกว่าหรือเท่ากับเกณฑ์ต่ำ โดยแต่ละรายการมีเกณฑ์ไม่เท่ากัน ลองดูผลลัพธ์ครับ
activity_main.xml
activity_main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" />row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" > <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:drawableLeft="@drawable/ico_apple" android:drawablePadding="10dp" /> <TextView android:id="@+id/num" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:paddingRight="20dp" /> </LinearLayout>
จากตัวอย่างที่แล้วเราได้ใส่รูปลงใน ListActivity ของเราแล้วแต่ว่าทุกๆรายการเป็นรูปเดียวกันหมด วันนี้เราจะมาสร้าง ListActivity ที่แต่ละรายการสามารถใช้รูปที่แตกต่างกันได้ โดยใช้ SimpleAdapter (ชื่อ Simple แต่ไม่แน่ใจว่า Simple จริงหรือป่าว 555) ก่อนอื่นดูผลลัพธ์โปรแกรมก่อนครับ
activity_main.xml
activity_main.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" />row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" /> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" /> </LinearLayout>MainActivity.java
package com.simplelist; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends ListActivity { final String[] fruit = {"Apple", "Banana", "Coconut", "Durian", "Guava", "Mango", "Mangosteen", "Rambutant"}; final int[] img = {R.drawable.ico_apple, R.drawable.ico_banana, R.drawable.ico_coconut, R.drawable.ico_durian, R.drawable.ico_guava, R.drawable.ico_mango, R.drawable.ico_mangosteen, R.drawable.ico_rambutant }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList<HashMap<String, Object>> listArg = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> map = null; int i = 0; for(String f : fruit){ map = new HashMap<String, Object>(); map.put("name", f); map.put("img", img[i]); listArg.add(map); i++; } SimpleAdapter adapter = new SimpleAdapter(this, listArg, R.layout.row, new String[]{"name", "img"}, new int[]{R.id.name, R.id.img}); this.setListAdapter(adapter); } @Override public void onListItemClick(ListView lv, View v, int position, long id){ Toast.makeText(this, "You click " + fruit[position], Toast.LENGTH_SHORT).show(); } }
วันนี้จะมาลองเขียนโปรแกรมรับส่งค่าระหว่าง activity แบบง่ายใน android กันครับ ก่อนอื่นเลยต้องรู้จักกับ activity ก่อน โดย activity เนี่ยถ้าพูดให้เข้าใจง่ายๆ ในตอนนี้อาจจะคิดว่า หนึ่งหน้าจอ คือ 1 activity ก่อนก็ได้ครับ ทีนี้เวลาเราเปิดแอพขึ้นมาหนึ่งแอพเนี่ย มันมีหลายๆ activity โดยเมื่อเปิด activity ขึ้นมาก็คือการ push ลงไปใน stack เมื่อ activity ทำงานเสร็จแล้วก็ pop ออกจาก stack ประมาณนี้อะครับ ถ้าต้องการเข้าใจมากขึ้นแนะนำให้ไปอ่าน ที่นี่ครับ
ทีนี้มาเข้าเรื่องของโปรเจ็คเรา โปรเจ็คนี้จะมี 2 activity โดย MainActivity จะโชว์หน้าจอให้ผู้ใช้กรอกชื่อเข้ามา จากนั้นจะทำการส่งชื่อไป DisplayActivity เพื่อไปแสดงครับ ก่อนอื่นก็สร้างโปรเจ็คขึ้นมาใหม่ครับ
ในที่นี้ใช้ชื่อโปรเจ็คว่า SampleIntent นะครับจะเปลี่ยนไปจากนี้ก็ได้ไม่ว่ากัน ในตอนที่สร้างโปรเจ็คใหม่ผมเลือกให้สร้าง Activity มาให้เลยคือ MainActivity( ชื่ออาจแก้ไขเป็นอันอื่นก็ได้ครับ ) ก็ต้องมาสร้าง activity อีกอันขึ้นมาโดยคลิกขวาที่ src/ชื่อแพคเกจ แล้วเลือก new -> class
จากนั้นก็สร้าง layout ขึ้นมาอีกหนึ่งไฟล์ ( ไฟล์ activity_main.xml ถูกสร้างมาพร้อมกับ MainActivity) โดยใช้ชื่อไฟล์ว่า display_activity.xml
จากนั้นก็มาแก้ไขแต่ละไฟล์เลยครับ
จากนั้นก็สร้าง layout ขึ้นมาอีกหนึ่งไฟล์ ( ไฟล์ activity_main.xml ถูกสร้างมาพร้อมกับ MainActivity) โดยใช้ชื่อไฟล์ว่า display_activity.xml
จากนั้นก็มาแก้ไขแต่ละไฟล์เลยครับ
สมัครสมาชิก:
บทความ
(
Atom
)