刚开始学习,懵懂的,遇到就网上找答案。
就说Gallery:
1。建立项目,把用到的图片存放到res/drawable目录。
2。res/layout/Activity_main.xml文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/aa" >
</ImageView>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="5dp" >
</Gallery>
</FrameLayout>
3.在res/values/目录中新建一个attrs.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground"></attr>
</declare-styleable>
</resources>
4.主类如下:
package com.example.gallery;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
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 {
@Override
public 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(getApplicationContext(), "" + position,
Toast.LENGTH_LONG).show();
//设置背景部分的ImageView显示当前Item的图片
iv.setImageResource(((ImageView)view).getId());
}
});
}
//定义继承BaseAdapter的匹配器
public class ImageAdapter extends BaseAdapter {
//Item的修饰背景
int mGalleryItemBackground;
//上下文对象
private Context mContext;
//图片数组
private Integer[] mImageIds = { R.drawable.b,
R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.g,
R.drawable.h, R.drawable.m,
R.drawable.l, R.drawable.k,
R.drawable.i };
//构造方法
public ImageAdapter(Context c){
mContext = c;
//读取styleable资源
// TypedArray自定义标签的使用;获得自定义属性,存放在a中。
TypedArray a = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
// 获取自定义属性id,后面使用自定义属性,通过id指定
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
//返回项目数量
@Override
public int getCount() {
return mImageIds.length;
}
//返回项目
@Override
public Object getItem(int position) {
return position;
}
//返回项目Id
@Override
public long getItemId(int position) {
return position;
}
//返回视图
@Override
public 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));
// 控制为了使图片适合 ImageView 的大小,FIT_XY为拉伸
iv.setScaleType(ImageView.ScaleType.FIT_XY);
// 为画廊添加主题风格。通过id指定
iv.setBackgroundResource(mGalleryItemBackground);
return iv;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
就说Gallery:
1。建立项目,把用到的图片存放到res/drawable目录。
2。res/layout/Activity_main.xml文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/aa" >
</ImageView>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:spacing="5dp" >
</Gallery>
</FrameLayout>
3.在res/values/目录中新建一个attrs.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="HelloGallery">
<attr name="android:galleryItemBackground"></attr>
</declare-styleable>
</resources>
4.主类如下:
package com.example.gallery;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
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 {
@Override
public 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(getApplicationContext(), "" + position,
Toast.LENGTH_LONG).show();
//设置背景部分的ImageView显示当前Item的图片
iv.setImageResource(((ImageView)view).getId());
}
});
}
//定义继承BaseAdapter的匹配器
public class ImageAdapter extends BaseAdapter {
//Item的修饰背景
int mGalleryItemBackground;
//上下文对象
private Context mContext;
//图片数组
private Integer[] mImageIds = { R.drawable.b,
R.drawable.c, R.drawable.d,
R.drawable.e, R.drawable.g,
R.drawable.h, R.drawable.m,
R.drawable.l, R.drawable.k,
R.drawable.i };
//构造方法
public ImageAdapter(Context c){
mContext = c;
//读取styleable资源
// TypedArray自定义标签的使用;获得自定义属性,存放在a中。
TypedArray a = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
// 获取自定义属性id,后面使用自定义属性,通过id指定
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
//返回项目数量
@Override
public int getCount() {
return mImageIds.length;
}
//返回项目
@Override
public Object getItem(int position) {
return position;
}
//返回项目Id
@Override
public long getItemId(int position) {
return position;
}
//返回视图
@Override
public 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));
// 控制为了使图片适合 ImageView 的大小,FIT_XY为拉伸
iv.setScaleType(ImageView.ScaleType.FIT_XY);
// 为画廊添加主题风格。通过id指定
iv.setBackgroundResource(mGalleryItemBackground);
return iv;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}