<Gallery android:id=“@+id/img_gallery” android:layout_width=“fill_parent”
android:layout_height=“110px” android:layout_marginTop=“10px”
android:layout_alignParentLeft=“true”/>
<ImageSwitcher android:id=“@+id/imageswitch”
android:layout_width=“90px” android:layout_height=“90px”
android:layout_alignParentTop=“true” android:layout_centerHorizontal=“true”
android:layout_alignBottom=“@+id/gallery”/>
2、完整类AddUserActivity.java,当点击对应的按钮的时候,就弹出圣诞框。
public class AddUserActivity extends Activity {
/** Called when the activity is first created. */
ImageButton imButton;
ImageSwitcher imageSwitcher;
Gallery gallery;
int imagePosition;
/**
- 所有的图像图片
*/
private int[] images
= new int[]{R.drawable.icon
,R.drawable.image1,R.drawable.image2,R.drawable.image3
,R.drawable.image4,R.drawable.image5,R.drawable.image6
,R.drawable.image7,R.drawable.image8,R.drawable.image9
,R.drawable.image10,R.drawable.image11,R.drawable.image12
,R.drawable.image13,R.drawable.image14,R.drawable.image15
,R.drawable.image16,R.drawable.image17,R.drawable.image18
,R.drawable.image19,R.drawable.image20,R.drawable.image21
,R.drawable.image22,R.drawable.image23,R.drawable.image24
,R.drawable.image25,R.drawable.image26,R.drawable.image27
,R.drawable.image28,R.drawable.image29,R.drawable.image30};
AlertDialog imageChooseDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_new);
imButton = (ImageButton) findViewById(R.id.image_button);
imButton.setOnClickListener(new ImageButtonListener());
}
class ImageButtonListener implements OnClickListener{
final CharSequence[] items = { “Red”, “Green”, “Blue” };
@Override
public void onClick(View v) {
initImageChooseDialog();
imageChooseDialog.show();
}
}
//初始化对话框;
private void initImageChooseDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(“请选择图片”)
.setPositiveButton(“确定”, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
imButton.setImageResource(images[imagePosition]);
}
})
.setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.imageswitch, null);
gallery = (Gallery)view.findViewById(R.id.img_gallery);
gallery.setAdapter(new ImageAdapter(this));//设置gallery数据。
gallery.setSelection(images.length/2);
imageSwitcher = (ImageSwitcher)view.findViewById(R.id.imageswitch);
imageSwitcher.setFactory(new ImageViewFactory(this));
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
imagePosition=position;
imageSwitcher.setImageResource(images[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
builder.setView(view);
imageChooseDialog=builder.create();
}
/**
-
这个Adapter类表示Data、Adapter、View,Adapter表示 Data和View之间的纽带
-
每显示一列数据都会运行这个类.
*/
class ImageAdapter extends BaseAdapter{
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
/**
- 显示的数据列数。这里是可供选择的图片数量。表示画的次数,在创建类时首先运行。
*/
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println(“BaseAdapter-----getCount:”+images.length);
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println(“BaseAdapter-----getItem:”+position);
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println(“BaseAdapter-----getItemId:”+position);
return 0;
}
/**
-
返回自己定义试图;也可以在xml文件里进行定义,(这里定义 的是gallery里的数据的格式 。)
-
这里position表示当前选择列的位置。
-
每显示一列数据都会执行一次
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println(“BaseAdapter-----position:”+position);
ImageView iv = new ImageView(context);
iv.setImageResource(images[position%images.length]);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(80,80));
iv.setPadding(15, 10, 15, 10);
return iv;
}
}
class ImageViewFactory implements ViewFactory{
Context context;
public ImageViewFactory(Context context) {
this.context = context;
}
@Override
public View makeView() {
ImageView iv = new ImageView(context);