android之路Gallery 画廊
来源:互联网 发布:手机淘宝购物车打不开 编辑:程序博客网 时间:2024/05/16 10:53
Gallery是一个内部元素可以水平滚动,并且可以把当前选择的子元素定位在它中心的布局组件。
我们还是直接看看例子的运行效果。
1.新疆项目HelloGallery
2.把需要展示的图片放入res/drawable目录。woman01.jpg,woman02.jpg,woman03.jpg
3.res/layout/activity_main.xml文件的内容如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/FrameLayout01" ><ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/ImageView01" android:src="@drawable/woman01"/><Gallery android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/gallery01" android:spacing="5dp"/></FrameLayout >
我们使用FrameLayout来实现叠加效果,使用ImageView来显示大图,Gallery来展示画廊,android:spacing="5dp" 属性则是用来设置元素之间的间隔。
4.在res/values/目录中新建一个attrs.xml内容如下:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="HelloGallery"> <attr name="android:galleryItemBackground"> </attr> </declare-styleable></resources>5.主activity,MainActivity的代码如下:
<pre name="code" class="java">package com.howlaa.hellogallery;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final ImageView iv = (ImageView)findViewById(R.id.ImageView01);Gallery g = (Gallery) findViewById(R.id.gallery01);//设置图片匹配器 g.setAdapter(new ImageAdapter(this)); //设置AdapterView点击监听器,Gallery是AdapterView的子类 g.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //显示点击的是第几张图片 Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_LONG).show(); //设置背景部分的ImageView显示当前Item的图片 iv.setImageResource(((ImageView)view).getId()); } });} }
6.ImageAdapter的代码如下:
package com.howlaa.hellogallery;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;import android.widget.SpinnerAdapter;public class ImageAdapter extends BaseAdapter {//Item的修饰背景 int mGalleryItemBackground; //上下文对象 private Context mContext; //图片数组 private Integer[] mImageIds ={R.drawable.woman01,R.drawable.woman02,R.drawable.woman03}; //构造方法 public ImageAdapter(Context c){ mContext = c; //读取styleable资源 TypedArray a = mContext.obtainStyledAttributes(R.styleable.HelloGallery); mGalleryItemBackground = a.getResourceId( R.styleable.HelloGallery_android_galleryItemBackground, 0); a.recycle(); } @Overridepublic int getCount() { return mImageIds.length;}@Overridepublic Object getItem(int position) { return position;}@Overridepublic long getItemId(int position) { return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ImageView iv = new ImageView(mContext);iv.setImageResource(mImageIds[position]); //给生成的ImageView设置Id,不设置的话Id都是-1 iv.setId(mImageIds[position]); iv.setLayoutParams(new Gallery.LayoutParams(120, 160)); iv.setScaleType(ImageView.ScaleType.FIT_XY); iv.setBackgroundResource(mGalleryItemBackground);return iv;}}
1 0
- android之路Gallery 画廊
- Android控件之Gallery(画廊)
- android开发之gallery(画廊)
- Android 控件之十二:Gallery画廊
- android 画廊 gallery
- Android--Gallery,画廊
- Android---画廊(Gallery)
- Android gallery画廊
- Android中的画廊(Gallery)
- Android Gallery画廊
- 【Android 开发】:UI控件之 Gallery 画廊控件的使用
- Android开发学习之Gallery画廊的使用
- Android开发学习之基于ViewPager实现Gallery画廊效果
- Android开发学习之基于ViewPager实现Gallery画廊效果
- Android基础--Gallery画廊控件
- android gallery实例(画廊效果)
- android gallery 画廊控件示例
- android学习---Gallery画廊视图
- base256-chinese约定以及csharp源代码
- 使用js判断手机端和pc端浏览器内核
- jetty minGzipSize 没有用
- Win8添加虚拟网卡的步骤
- 【Git使用详解】EGit使用详解
- android之路Gallery 画廊
- Windows7中 .mongorc.js 文件的位置
- 黑马程序员__IO
- 初步的学习HTML
- Paint常用方法----SpannableString属性
- IOS 自动补全Email的TextField
- 一个UDP异步通信例子
- 桶排序
- Linux Shell 之比较运算符