Gallery使用教程——尝试翻译一篇Android SDK Reference

Gallery使用教程
Gallery是一个布局工具,可以将其它控件组合在水平滚动条中,并且可以让当前选项的控件定位到布局的中间
在下面的教程中,你会创建一个显示照片的Gallery,并且每一个条目被选中后它会显示相应的土司消息

[list]
[*]1新建项目,取名HelloGallery.
打开res/layout/main.xml文件,然后添加入以下代码

<?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"/>

[/list]
[list]
[*]2找一些你将要在项目中使用的图片
[/list]


[list]
[*]3打开HelloGalerry.java文件,在onCreate()方法中,添加入以下代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));

gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
[/list]
[list]
[*]4在res/values/目录下建一个xml文档,命名为arrts.xml,插入下面的代码

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

这是一个用户自定义的styleable资源,可以用于layout中。在本例中,它将运用与Gallery 中每一个独立的item
这个<attr>元素定义某个styleable的attribute属性,其实,在这里,它指向了一个平台中已经预定义的属性,这个属性就是galleryItemBackgroud,在原文中我们可以看到这样的解释The preferred background for gallery items. This should be set as the background of any Views you provide from the Adapter. 其实就是说这个galleryItemBackgroud是多数情况下专门为gallery items设定的背景
下一步,你将会看见这个galleryItemBackgroud是如何被引用,以及如何被使用于gallery中的每一个item的。
[/list]
[list]
[*]5.回到HelloGallery.java文件中,在onCreate(Bundle)方法后面写入自定义的ImageAdapter类

public class ImageAdapter extends BaseAdapter {
// 用来设置Galley中的每一个Item的风格,也就是ImageView的风格
int mGalleryItemBackground;
private Context mContext;
//图片的资源ID
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};

public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
attr.recycle();
}
//返回所有图片的个数
public int getCount() {
return mImageIds.length;
}
//返回图片在资源的位置
public Object getItem(int position) {
return position;
}
//返回图片在资源的位置
public long getItemId(int position) {
return position;
}
//此方法是最主要的,他设置好的ImageView对象返回给Gallery
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
//通过索引获得图片并设置给ImageView
imageView.setImageResource(mImageIds[position]);
//设置布局参数
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
//设置ImageView的伸缩规格,用了自带的属性值
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//设置风格,此风格的配置是在xml中
imageView.setBackgroundResource(mGalleryItemBackground);

return imageView;
}
}

[/list]
[list]
[*]6.运行程序,你将看到如下效果
[img][/img]
[/list]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值