新建一View,清单如下:

view_gallery.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    </LinearLayout>


在面板拖拽图标 ,然后更改相关属性(Properties),如下:

<Gallery        android:id="@+id/gallery02"        android:layout_width="fill_parent"        android:layout_height="wrap_content"         android:layout_marginTop="30dp"/>


添加一按钮,如下:

<Button        android:id="@+id/btnReturn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="166dp"        android:text="@string/btn1Caption" />


整体布局效果如下:

把“Push me”按钮,更改为切换按钮。即实现切换功能。相关代码请参考 多个View切换!

实现Gallery需要继承BaseAdapter,我们命名为ImgAdapter。

代码清单如下:

package com.example.prjandroid;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 ImgAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn null;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}}


如果想要放图片,必须使用ImageView!

这时,我们把图片放到res/drawable下(建议使用png格式文件)。

然后,再新建一xml文件,提供Gallery的背景。如下:

res/attrs.xml

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


声明一图片ID的数组:

// resource drawprivate int[] resPics = new int[] {R.drawable.emacs1,R.drawable.emacs2,R.drawable.emacs3,R.drawable.emacs4,R.drawable.emacs5,R.drawable.emacs6,R.drawable.emacs7,R.drawable.emacs8,R.drawable.emacs9,R.drawable.emacs10};


在getView()方法中使用ImageView:

ImageView imgView = new ImageView(m_context);imgView.setImageResource(resPics[position]);imgView.setScaleType(ImageView.ScaleType.FIT_XY);imgView.setLayoutParams(new Gallery.LayoutParams(163, 106));imgView.setBackgroundResource(m_galleryItemBackGround);return imgView;

其中,ImageView.ScaleType共八种:

1·ImageView.ScaleType.center:图片位于视图中间,但不执行缩放。

2·ImageView.ScaleType.CENTER_CROP 按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者大于相应的视图的维度

3·ImageView.ScaleType.CENTER_INSIDE按统一比例缩放图片(保持图片的尺寸比例)便于图片的两维(宽度和高度)等于或者小于相应的视图的维度

4·ImageView.ScaleType.FIT_CENTER缩放图片使用center

5·ImageView.ScaleType.FIT_END缩放图片使用END

6·ImageView.ScaleType.FIT_START缩放图片使用START

7·ImageView.ScaleType.FIT_XY缩放图片使用XY

8·ImageView.ScaleType.MATRIX当绘制时使用图片矩阵缩放

还有就是Context相当于windows中的Handle。

我们在构造这个类时,需要Context和初始化Gallery的属性,其相关代码如下:

public ImgAdapter(Context context) {// TODO Auto-generated constructor stubm_context = context;TypedArray typeArray = m_context.obtainStyledAttributes(R.styleable.galleryThem);m_galleryItemBackGround = typeArray.getResourceId(R.styleable.galleryThem_android_galleryItemBackground, 0);}


基本上这个适配类就是这个样子了。

调用代码和之前讲的Spinner比较像!区别在于Spinner直接就传的this,但是在必须传主类.this(例如MainActivity. this)。

运行效果如下:

作者:xiaobin_HLJ80 发表于2012-9-15 0:41:21 原文链接 阅读:28 评论:0 查看评论

更多相关文章

  1. android属性收集
  2. android最佳实践(三)
  3. Android菜鸟日记17-SeekBar的图片效果
  4. Android字体大小与titile标题居中设置
  5. Android基础04-基本控件及表单三大控件
  6. Android(安卓)GridView属性
  7. Android使用两个Activity页面切换…
  8. button按钮中的文字和drawableLeft的图片距离太远
  9. android背景图片平铺

随机推荐

  1. Android(安卓)Studio 中Kotlinx开发
  2. Android调用接口,获取并解析数据(json格式)
  3. Android(安卓)AlertDialog ————普通
  4. Android(安卓)短信转换成彩信的消息数量(
  5. TEE normal world侧实现分析
  6. Android(安卓)输入系统(三)InputReader
  7. [Android面试题-3] Activity的四种加载模
  8. Plugin-X Integration Guide for Android
  9. Google Android尝鲜 - 计算器小程序
  10. View高度动画