1,xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Gallery android:id="@+id/gallery" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout> |
2,activity代码
public class MainActivity extends Activity { private int[]images = new int[]{ R.drawable.cat, R.drawable.cat1, R.drawable.cattt, R.drawable.cat, R.drawable.cat1, R.drawable.cattt}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new MyAdapter()); } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return images.length; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if(convertView == null){ imageView = new ImageView(getApplicationContext()); } else { imageView = (ImageView) convertView; } imageView.setImageResource(images[position]); return imageView; } } } |
原文地址:http://9882931.blog.51cto.com/9872931/1613850