1,创建配置文件

<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/gallery"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    ></Gallery>

2,编写适配器,因为Gallery 需要一个适配器填充,而且填充的必须输图片,所以。。。

package com.kang.fei.gallery;import android.content.Context;import android.content.res.TypedArray;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {private Context context;int mGalleryItemBackground;private Integer[] mImageids ={R.drawable.sample_0,R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_4,R.drawable.sample_5,R.drawable.sample_6};public ImageAdapter(Context context){this.context = context;//TypedArray实例是个属性的容器,context.obtainStyledAttributes()方法返回得到TypedArray a = context.obtainStyledAttributes(R.styleable.HelloGallery);mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0);a.recycle();}public int getCount() {return mImageids.length;}public Object getItem(int position) {return position;}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {ImageView i = new ImageView(context);i.setImageResource(mImageids[position]);i.setLayoutParams(new Gallery.LayoutParams(150,100));i.setScaleType(ImageView.ScaleType.FIT_XY);i.setBackgroundResource(mGalleryItemBackground);return i;}}

3,编写一个attrs.xml文件,防止在values文件颊下

<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="HelloGallery">    <attr name="android:galleryItemBackground"/></declare-styleable></resources>

4,编写主层序类

package com.kang.fei.gallery;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Gallery;import android.widget.Toast;public class GalleryActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Gallery g = (Gallery) findViewById(R.id.gallery);        g.setAdapter(new ImageAdapter(this));        g.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(GalleryActivity.this, " "+position,1).show();}        });    }}

更多相关文章

  1. Android 利用 xml 文件实现 ImageView 的加载转圈动画
  2. 【转】Android xml资源文件中@、@android:type、@*、?、@+含义和
  3. 传智播客—Android(二)数据存储和访问 之文件
  4. Android夸进程通信机制五:使用文件共享进行进程间通信

随机推荐

  1. android常用框架
  2. android attr.xml文件
  3. Android: Linear Layout and weight
  4. Android用户权限大全
  5. ToggleButton
  6. Android 画椭圆
  7. 开发者必看|Android(安卓)8.0 新特性及开
  8. Maven Android Plugin
  9. android颜色代码
  10. 【Android】自定义权限