Main.axml:就只有一個 ListView 控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px"> <ListView android:minWidth="25px" android:minHeight="25px" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listView1" /> </LinearLayout>CustomListItem.axml:相當於 ListView Item,放一個 ImageView 並在 TableLayout 內放進四個 TextView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:src="@android:drawable/ic_menu_gallery" android:layout_width="match_parent" android:layout_height="123.0dp" android:id="@+id/imPhoto" /> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1" android:stretchColumns="*"> <TableRow android:id="@+id/tableRow1"> <TextView android:text="姓名" android:layout_column="0" android:id="@+id/txtName" android:textSize="20dp" /> <TextView android:text="性別" android:id="@+id/txtGender" android:textSize="20dp" /> </TableRow> <TableRow android:id="@+id/tableRow2"> <TextView android:text="到職日" android:layout_column="0" android:id="@+id/txtHireDate" android:textSize="20dp" /> <TextView android:text="生日" android:id="@+id/txtBirthDate" android:textSize="20dp" /> </TableRow> </TableLayout> </LinearLayout>MainActivity.cs
namespace ListViewBase { [Activity(Label = "ListViewBase", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); List<Personalinfo> data = new List<Personalinfo>(); data.Add(new Personalinfo() { Name = "JT", Gender = "男", HireDate = new DateTime(2008, 12, 11), BirthDate = new DateTime(1981, 12, 11), Photo = Resource.Drawable.ListView1 }); data.Add(new Personalinfo() { Name = "SH", Gender = "女", HireDate = new DateTime(2011, 11, 11), BirthDate = new DateTime(1980, 1, 9) , Photo = Resource.Drawable.ListView2 }); data.Add(new Personalinfo() { Name = "TY", Gender = "男", HireDate = new DateTime(2016, 6, 27), BirthDate = new DateTime(2014, 5, 5), Photo = Resource.Drawable.ListView3 }); CustomeAdapter adapter = new CustomeAdapter(this, data); ListView list = FindViewById<ListView>(Resource.Id.listView1); list.Adapter = adapter; } } public class CustomeAdapter : BaseAdapter<Personalinfo> { private Activity _Context; private List<Personalinfo> _Data; public CustomeAdapter(Activity context, List<Personalinfo> data) { this._Context = context; this._Data = data; } public override int Count { get { return _Data.Count; } } public override Personalinfo this[int position] { get { return _Data[position]; } } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) v = _Context.LayoutInflater.Inflate(Resource.Layout.CustomListItem, null); v.FindViewById<TextView>(Resource.Id.txtName).Text = $"姓名:{_Data[position].Name}"; v.FindViewById<TextView>(Resource.Id.txtGender).Text = $"性別:{_Data[position].Gender}"; v.FindViewById<TextView>(Resource.Id.txtBirthDate).Text = $"生日:{_Data[position].BirthDate.ToString("yyyy/MM/dd")}"; v.FindViewById<TextView>(Resource.Id.txtHireDate).Text = $"到職日:{_Data[position].HireDate.ToString("yyyy/MM/dd")}"; v.FindViewById<ImageView>(Resource.Id.imPhoto).SetImageResource(_Data[position].Photo); return v; } } public class Personalinfo { public string Name { get; set; } public string Gender { get; set; } public DateTime HireDate { get; set; } public DateTime BirthDate { get; set; } public int Photo { get; set; } } }
- Drawable 內圖片和 Resource.Designer
沒有留言:
張貼留言