ListViewに0~99の番号を画像で表示する。
全角の大きさの画像0~9(a0.png~a9.png)と半角の大きさの画像0~9(b0.png~b9.png)を用意する。
1桁はa0.png~a9.pngを用い、2桁はb0.png~b9.pngを組み合わせて表示する。
■1行のレイアウト
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:ignore="UseCompoundDrawables" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" > <ImageView android:id="@+id/ImageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:contentDescription="@string/description"/> <ImageView android:id="@+id/ImageView1_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:contentDescription="@string/description"/> <ImageView android:id="@+id/ImageView1_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:contentDescription="@string/description"/> <TextView android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world"/> </LinearLayout>
■カスタムアダプター
onCreateのタイミングでリソースから画像を読み込み、メンバに設定しておく。
mNumImgsA = new Drawable[10]; mNumImgsB = new Drawable[10]; for (int i = 0; i <= 9; i++) { int id = getResources().getIdentifier("a" + i, "drawable", getPackageName()); mNumImgsA [i] = getResources().getDrawable(id); } for (int i = 0; i <= 9; i++) { int id = getResources().getIdentifier("b" + i, "drawable", getPackageName()); mNumImgsB [i] = getResources().getDrawable(id); } ImageView imageView = (ImageView)v.findViewById(R.id.ImageView1); ImageView imageView_left = (ImageView)v.findViewById(R.id.ImageView1_left); ImageView imageView_right = (ImageView)v.findViewById(R.id.ImageView1_right); if (position < 10) { imageView.setVisibility(View.VISIBLE); imageView_left.setVisibility(View.GONE); imageView_right.setVisibility(View.GONE); imageView.setImageDrawable(mNumImgsA[position]); } else { int doubleDigit = position / 10; int singleDigit = position % 10; imageView.setVisibility(View.GONE); imageView_left.setVisibility(View.VISIBLE); imageView_right.setVisibility(View.VISIBLE); imageView_left.setImageDrawable(mNumImgsB[doubleDigit]); imageView_right.setImageDrawable(mNumImgsB[singleDigit]); }
コメント