可以比較這篇文章和網格視圖(GridView)的源碼的區別, 主要是在getView()之上.
先創建一個文件夾/res/drawable, 並複製一些PNG圖形到/res/drawable內. 我使用的文件名為a01.png~a15.png.
修改main.xml, 使用Gallery.
<?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"
>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:spacing="10dp"
/>
</LinearLayout>
修改源碼.
package com.AndroidGallery;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class AndroidGallery extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery myGallery = (Gallery)findViewById(R.id.gallery);
myGallery.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter{
private Context context;
private Integer[] mThumbIds = {
R.drawable.a01,
R.drawable.a02,
R.drawable.a03,
R.drawable.a04,
R.drawable.a05,
R.drawable.a06,
R.drawable.a07,
R.drawable.a08,
R.drawable.a09,
R.drawable.a10,
R.drawable.a11,
R.drawable.a12,
R.drawable.a13,
R.drawable.a14,
R.drawable.a15,
};
public ImageAdapter(Context c){
context = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
}
下一篇文章: 實現Gallery的OnItemSelectedListener.
可以示範 WebView 嗎?
回覆刪除有一個app叫 "Finance" 可以用它來示範WebView嗎?
回覆刪除已經上了:
回覆刪除http://androidbiancheng.blogspot.com/2010/01/webview.html
http://androidbiancheng.blogspot.com/2010/01/webview_11.html
但是, 你提到那個App看似Feed Reader, 不像是WebView!