Dialog中Gallery的使用方法

Dialog 有两种使用方法:

一:在Activity中重写onCreateDialog(int id)的方法来创建Dialog的对象并返回,然后调用Activity中的showDialog(int id)和DismissDialog(int id)来显示和隐藏Dialog对话框

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder dialog=null;
		if(id==100){
			LinearLayout ll = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.picturelayout, null);
			gallery = (Gallery)ll.findViewById(R.id.gallery1);	
			gallery.setAdapter(ia);
			gallery.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					str = ia.Name(arg2);
				}		
			});
			dialog = new AlertDialog.Builder(this).setView(ll);
			dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					str=0;
				}
			});
			
		}
		return dialog.create();
	}

用上面这种方法创建时,注意如果要创建Button的click事件的话,必须创建Builder对象,让后用Builder对象去创建Button,然后返回dialog.create();

然后在oncreate()方法中调用showDialog(100);就行。


二:直接new一个Dialog对象dialog 然后调用show()和dismiss()方法

	public void check(){
			AlertDialog.Builder builder = new AlertDialog.Builder(SavePictureActivity.this);     
			final View textEntryView = LayoutInflater.from(this).inflate(R.layout.picturelayout, null);  
		    
//			LinearLayout ll = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.picturelayout, null);
			
			gallery = (Gallery)textEntryView.findViewById(R.id.gallery1);	
			gallery.setAdapter(ia);
			gallery.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					str = ia.Name(arg2);
				}		
			});  
		     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
		         public void onClick(DialogInterface dialog, int whichButton) {  
		             
		         }  
		     });  
		     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
		         public void onClick(DialogInterface dialog, int whichButton) {  
		        	 str=0;
		         }  
		     });  
		   builder.setView(textEntryView).create().show();
	}

注意我刚开始创建的是Builder对象builder  直到builder.setView(textEntryView).creat()  调用了creat()才创建了Dialog的对象,然后调用show()方法显示Dialog对话框

上面创建View对象时我用了不同的方法

final View textEntryView = LayoutInflater.from(this).inflate(R.layout.picturelayout, null);

LinearLayout ll = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.picturelayout, null);

两种都可以,但是推荐用第一种方法,因为第一种方法更大众一些,有时候你的布局可能不是线性布局。


Gallery的用法:

在你的调用方法或oncreate()方法中调用一下代码就行:

                        ImageAdapter ia = new ImageAdapter(this);			
                        gallery = (Gallery)textEntryView.findViewById(R.id.gallery1);	
			gallery.setAdapter(ia);
			gallery.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					str = ia.Name(arg2);
				}		
			});  

先找到控件gallery ,然后将数据放进gallery适配器中。

ImageAdapter的页面代码如下:

public class ImageAdapter extends BaseAdapter{

	Context context;
    public Integer[] myImageIds = {R.drawable.auto1, R.drawable.auto2,R.drawable.auto3,R.drawable.auto4,
    		R.drawable.auto5,R.drawable.bike,R.drawable.bike1};
    
	public ImageAdapter(Context context){
		this.context = context;
	}
	@Override
	public int getCount() {
		return myImageIds.length;
	}
	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView iv = new ImageView(context);
		iv.setImageResource(myImageIds[position]);
		iv.setScaleType(ImageView.ScaleType.FIT_XY);
	    iv.setLayoutParams(new Gallery.LayoutParams(136, 88));  
	    iv.setBackgroundColor(Color.WHITE); 
	    return iv;  
	}
	public int Name(int id){
		return myImageIds[id];
	}
}

具体Gallery的各种用法可以参考博客: Android中的对话框AlertDialog使用技巧合集.



你可能感兴趣的:(Dialog中Gallery的使用方法)