111544965  

mian.xml:(只有一个Gallery)

1
2
3
4
5
6
7
8
9
10
11
12
<LinearLayout 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="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
       />
</LinearLayout>

接下来是cell.xml文件:(只有一个ImageView)

1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    <ImageView
        android:id="@+id/ce_im"
        android:layout_width="match_parent"
        android:layout_height="match_parent"    
        />
</LinearLayout>

接下来是自定义的Adapter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class MyAdapter extends BaseAdapter {
    private Context context;
    private int[] imageids;
    public MyAdapter(Context context, int[] imageids) {
        super();
        this.context = context;
        this.imageids = imageids;
    }
    @Override
    public int getCount() {
        return imageids.length;
    }
    @Override
    public Object getItem(int position) {
        return imageids[position];
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //以下注释部分为动态的添加View(直接在代码中定义视图控件。不需要xml文件,可根据个人习惯选择),
         /*ImageView imageView=new ImageView(context);
         imageView.setImageResource(imageids[position]);
         imageView.setLayoutParams(new
         Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
         LayoutParams.WRAP_CONTENT));*/
                                                                                      
        ImageView imageView;
        if (convertView == null) {
            //获取视图
            convertView = LayoutInflater.from(context).inflate(R.layout.cell,
                    null);
            imageView = (ImageView) convertView.findViewById(R.id.ce_im);
            imageView.setImageResource(imageids[position]);
        }else {
            imageView = (ImageView) convertView.findViewById(R.id.ce_im);
            imageView.setImageResource(imageids[position]);
        }
        return convertView;
    }
}

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class MainActivity extends Activity {
    private Gallery gallery;
    private MyAdapter myadapter;
    private int[] images={R.drawable.i1,R.drawable.i2,
            R.drawable.i3,R.drawable.i4}; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gallery=(Gallery) findViewById(R.id.gallery);     
        myadapter=new MyAdapter(this, images);
        gallery.setAdapter(myadapter);            
    }
}
 
 
 
 
本文出自 “天马行空” 博客,请务必保留此出处http://shaotao.blog.51cto.com/7606729/1281323

 

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()