介绍
该控件已经过时,Gallery控件在页面展示上的效果的是不如ViewPager(官方还推荐HorizontalScrollView)的,比如说,在图片的衔接上,Gallery是属于那种拖泥带水的,而ViewPager是干净利落的。
画廊视图(Gallery)表示,能够按水平方向显示内容,并且可用手指直接拖动图片移动,一般用来浏览图片,被选中的选项位于中间,并且可以响应事件显示信息。在使用画廊视图时,首先需要在屏幕上添加 Gallery 组件,通常使用 <Gallery> 标记在 XML 布局文件中添加,其基本语法如下:
< Gallery
属性列表
>
</Gallery>
XML属性
Gallery 支持的 XML 属性
XML属性 | 描述 |
android:animationDuration | 用于设置列表项切换时的动画持续时间 |
android:gravity | 用于设置对齐方式 |
android:spacing | 用于设置列表项之间的间距 |
android:unselectedAlpha | 用于设置没有选中的列表项的透明度 |
例子
模拟淘宝搜索宝贝时的提示列表
编写布局文件
<?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="vertical">
<Gallery
android:id="@+id/gallery1"
android:spacing="5dp"
android:unselectedAlpha="0.6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
创建attr.xml文件
在 res\values 目录中点击鼠标右键 New → Values resource file,创建一个名称为 attr.xml的文件,在该文件中定义一个 styleable 对象,用于组合多个属性。这里只指定了一个系统自带的android:galleryItemBackground 属性,用于设置各选项的背景,具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
编写GalleryActivity
public class GalleryActivity extends AppCompatActivity {
//定义并初始化保存图片id的数组
private int[] imageId = new int[] {
R.drawable.img01, R.drawable.img02, R.drawable.img03,
R.drawable.img04, R.drawable.img05, R.drawable.img06,
R.drawable.img07, R.drawable.img08, R.drawable.img09,
R.drawable.img10, R.drawable.img11, R.drawable.img12,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
Gallery gallery = (Gallery) findViewById(R.id.gallery1); //获取Gallery组件
BaseAdapter adapter = new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview; //声明ImageView的对象
if (convertView == null) {
imageview = new ImageView(GalleryActivity.this);//实例化ImageView的对象
imageview.setScaleType(ImageView.ScaleType.FIT_XY);//设置缩放方式
imageview.setLayoutParams(new Gallery.LayoutParams(300, 200));
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
imageview.setBackgroundResource(typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,0));
imageview.setPadding(5, 0, 5, 0); //设置ImageView的内边距
} else {
imageview = (ImageView) convertView;
}
imageview.setImageResource(imageId[position]); //为ImageView设置要显示的图片
return imageview; //返回ImageView
}
/*
* 功能:获取当前选项的id
*/
@Override
public long getItemId(int position) {
return position;
}
/*
* 功能:获取当前选项
*/
@Override
public Object getItem(int position) {
return position;
}
/*
* 获取数量
*/
@Override
public int getCount() {
return imageId.length;
}
};
gallery.setAdapter(adapter); //将适配器与Gallery关联
gallery.setSelection(imageId.length / 2); //选中中间的图片
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(GalleryActivity.this,"您选择了第" + String.valueOf(position) + "张图片",
Toast.LENGTH_SHORT).show();
}
});
}
}
效果
bug:只有一开始打开第一张时不是透明的,之后都是透明的