2010年12月20日月曜日

Android GridView でアイコンと文字のレイアウト

# メール書こうかなぁと思ったんだけど、なんか解決したみたいなので、こっちに書くことにしたよ。



MyActivity.java

package yanzm.example.gridviewsample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MyActivity extends Activity {

public class BindData {
int iconId;
String title;

BindData(int id, String s) {
this.iconId = id;
this.title = s;
}
}

private BindData[] mDatas = {
new BindData(android.R.drawable.ic_menu_camera, "Camera"),
new BindData(android.R.drawable.ic_menu_add, "Add"),
new BindData(android.R.drawable.ic_menu_agenda, "Agenda"),
new BindData(android.R.drawable.ic_menu_call, "Call"),
new BindData(android.R.drawable.ic_menu_crop, "Compass"),
new BindData(android.R.drawable.ic_menu_camera, "Crop"),
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyAdapter(this, R.layout.item, mDatas));
}

static class ViewHolder {
TextView textView;
ImageView imageView;
}

public class MyAdapter extends ArrayAdapter<BindData> {
private LayoutInflater inflater;
private int layoutId;

public MyAdapter(Context context, int layoutId, BindData[] objects) {
super(context, 0, objects);
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.layoutId = layoutId;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;

if (convertView == null) {
convertView = inflater.inflate(layoutId, parent, false);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textview);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BindData data= getItem(position);
holder.textView.setText(data.title);
holder.imageView.setImageResource(data.iconId);
return convertView;
}
}
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:columnWidth="85dip"
android:verticalSpacing="8dip"
android:stretchMode="columnWidth"
android:gravity="center"
/>


item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:gravity="center"
>
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>



 

1 件のコメント:

  1. i will try this.

    i cant fix my knowledge about your kanji, sorry. but thanks you type into latin. so far so thanks :D

    返信削除