当前位置:
首页 >
Android 的基本组件之一 Gallery
发布时间:2023/12/9
47
豆豆
生活随笔
收集整理的这篇文章主要介绍了
Android 的基本组件之一 Gallery
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
声明:本人博客纯属个人学习过程中的一些仿写的简单练习记录,其他论坛也有类似内容!(可能不免有错误之处,还望见谅,指出)
这是一个最简单可以滑动查看图片的应用程序:
视图布局文件 main xml : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:longClickable="true"> > <Gallery android:id="@+id/Picture" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> 应用配置文件 AndroidMainfest xml : <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.slide.demo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Slide" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
这是一个最简单可以滑动查看图片的应用程序:
首先创建项目名为: Gallery My photo
代码如下: package com.slide.demo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class Slide extends Activity { private Gallery mgallery; //设置Gallery对象名 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mgallery = (Gallery) findViewById(R.id.Picture); //联系到视图布局 ImageAdapter madapter = new ImageAdapter(this); //构建一个适配器 mgallery.setAdapter(madapter); mgallery.setSpacing(6); //设置图片之间的距离 } private class ImageAdapter extends BaseAdapter { int[] images = { R.drawable.image0, R.drawable.image1, R.drawable.image3, R.drawable.image4, R.drawable.image5, R.drawable.image2, }; //联系到drawable文件夹中要显示的图片(这里有6张) Context mContext; //构造函数 public ImageAdapter(Context ctx) { mContext = ctx; } public int getCount() { return images.length; } //得到图片的数量 public Object getItem(int position) { return images[position]; } //继承父类中的方法,获取图片数据中指定位置的设置 public long getItemId(int position) { return 0; } //继承父类中的方法,获取id和图片集里面的位置 @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = null; if (convertView == null) { iv = new ImageView(mContext); iv.setImageResource(images[position]); } else { iv = (ImageView) convertView; } return iv; //判断图片显示位置是否为空,若为空即设置下一张图片填充 } } }视图布局文件 main xml : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:longClickable="true"> > <Gallery android:id="@+id/Picture" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout> 应用配置文件 AndroidMainfest xml : <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.slide.demo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Slide" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
转载于:https://www.cnblogs.com/-cyb/archive/2010/11/26/Android_Gallery.html
总结
以上是生活随笔为你收集整理的Android 的基本组件之一 Gallery的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: WPF 2D绘图(2)Geometry
- 下一篇: 深入浅出Google Android这本