小弟的代码
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Gallery
android:id="@+id/gallery01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Main:import com.cn.BookSamples.R;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;public class Three_WidgetDemo extends Activity {
public void OnCreate(Bundle b){
super.onCreate(b);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(com.cn.BookSamples.R.layout.widget_sample3);Gallery g = (Gallery)findViewById(R.id.gallery01);
//添加ImageAdapter给Gallery对象
g.setAdapter(new ImageAdapter(this));
//设置gallery背景
g.setBackgroundResource(R.drawable.bg);//设置Gallery的事件监听
g.setOnItemClickListener(new OnItemClickListener() {@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(Three_WidgetDemo.this, "你选择了"+ (arg2+1)+"号图片", Toast.LENGTH_SHORT).show();
}});}
}
ImageAdapterpublic ImageAdapter(Context c){
m_Context=c;
TypedArray a = m_Context.obtainStyledAttributes(R.styleable.Gallery);
a.recycle();
}//定义Context
private Context m_Context;private int[] myImageIds = {
R.drawable.bg,
R.drawable.blue1,
R.drawable.editbox_background,
R.drawable.thumb_focused
};@Override
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return myImageIds[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(m_Context);
//给ImageView设置资源
imageView.setImageResource(myImageIds[arg0]);
//设置布局图片以120*120显示
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
//设置显示比例
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}小弟刚入门,还望高手指教!
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Gallery
android:id="@+id/gallery01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Main:import com.cn.BookSamples.R;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery;
import android.widget.Toast;public class Three_WidgetDemo extends Activity {
public void OnCreate(Bundle b){
super.onCreate(b);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(com.cn.BookSamples.R.layout.widget_sample3);Gallery g = (Gallery)findViewById(R.id.gallery01);
//添加ImageAdapter给Gallery对象
g.setAdapter(new ImageAdapter(this));
//设置gallery背景
g.setBackgroundResource(R.drawable.bg);//设置Gallery的事件监听
g.setOnItemClickListener(new OnItemClickListener() {@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(Three_WidgetDemo.this, "你选择了"+ (arg2+1)+"号图片", Toast.LENGTH_SHORT).show();
}});}
}
ImageAdapterpublic ImageAdapter(Context c){
m_Context=c;
TypedArray a = m_Context.obtainStyledAttributes(R.styleable.Gallery);
a.recycle();
}//定义Context
private Context m_Context;private int[] myImageIds = {
R.drawable.bg,
R.drawable.blue1,
R.drawable.editbox_background,
R.drawable.thumb_focused
};@Override
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return myImageIds[arg0];
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(m_Context);
//给ImageView设置资源
imageView.setImageResource(myImageIds[arg0]);
//设置布局图片以120*120显示
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
//设置显示比例
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageView;
}小弟刚入门,还望高手指教!
// TODO Auto-generated method stub
//new a ImageView for the image displaying
ImageView imgView = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeFile(mArrayList.get(position));
imgView.setImageBitmap(bm);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imgView.setBackgroundResource(mGalleryBackground);
return imgView;
}
我也不懂android应用,我是做驱动的。希望对你有帮助
public void OnCreate(Bundle b){ 应该为public void onCreate(Bundle b){
这个覆写方法最好是不要手写容易写错