《Gallery和自定义MyAdapter显示图片》由会员分享,可在线阅读,更多相关《Gallery和自定义MyAdapter显示图片(3页珍藏版)》请在金锄头文库上搜索。
1、MainActivity.javapackage com.example.aaa;import android.os.Bundle;import android.app.Activity;import android.widget.Gallery;public class MainActivity extends Activity Gallery gallery;Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout
2、.activity_main);gallery=(Gallery) findViewById(R.id.gallery1);gallery.setAdapter(new MyAdapter(this);gallery.setSelection(1);MyAdapter.javapackage com.example.aaa;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.wi
3、dget.Gallery;import android.widget.ImageView;public class MyAdapter extends BaseAdapter private Context mContext;private int imgs=R.drawable.mm0,R.drawable.mm1,R.drawable.mm2,;public MyAdapter(Context c) super();/ TODO Auto-generated constructor stubmContext=c;Overridepublic int getCount() / TODO Au
4、to-generated method stubreturn imgs.length;Overridepublic Object getItem(int position) / TODO Auto-generated method stubreturn position;Overridepublic long getItemId(int position) / TODO Auto-generated method stubreturn position;Overridepublic View getView(int position, View convertView, ViewGroup parent) / TODO Auto-generated method stubImageView iv=new ImageView(mContext);iv.setBackgroundResource(imgsposition);iv.setAdjustViewBounds(true);iv.setLayoutParams(new Gallery.LayoutParams(300, 250);return iv;main.xml 文档结束peng