android Gallery组件的一个想法 Button,ImageView都是TextView的孩子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一下是源码package com.ray.test;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Gallery;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class TestGallery extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g = (Gallery) findViewById(R.id.Gallery01);//get Gallery component g.setAdapter(new ImageAdapter(this));//set image resource for gallery //add listener g.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { //just a test,u can start a game activity Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show(); } }); }}=======================================================================================package com.ray.test;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.Gallery;//import android.widget.ImageView;//import android.widget.RelativeLayout.LayoutParams;import android.widget.ImageView;public class ImageAdapter extends BaseAdapter { private Context mContext; //define Context private Integer[] mImageIds = { //picture source R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, }; public ImageAdapter(Context c) { //define ImageAdapter mContext = c; } //get the picture number public int getCount() { return mImageIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) {// ImageView i=new ImageView(mContext); Button button=new Button(mContext); button.setBackgroundResource(mImageIds[position]); // button.setLayoutParams(new Gallery.LayoutParams(192,192));// i.setImageResource(mImageIds[position]);//set resource for the imageView button.setLayoutParams(new Gallery.LayoutParams(100, 100));//layout button.setPadding(10, 10, 10, 10); //i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type return button; }}理论上是可行的,但是不知道代码哪里写错了 imageview extends 自 view 可能还是内部机制不一样吧 不能滑动的原因是你鼠标移动到Button后,这个事件其实是传递给了Button来进行处理,并没有传递给Gallery,你设置下Button.setOnClickListener (false);那么Gallery 就能滑动了,另外setOnClickListener的时候不能在getView里面进行设置 请问解析JSON时,org.json包和gson包的区别 android gallery应用 请教android openGL 顶点坐标的问题 VirtualBox + android-x86 问题解决 Notification 怎么常驻? 请问一个安卓录音的问题? 只能在activity里边添加view么? android调用webservice后,更新UI 系统出错了,从哪里开始重载. Parcelable的问题 程序在后台运行,达到一定条件,就会震动和铃声提醒,能实现这样的功能吗? Android2.0 Camera问题
package com.ray.test;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;public class TestGallery extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.Gallery01);//get Gallery component
g.setAdapter(new ImageAdapter(this));//set image resource for gallery //add listener
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
//just a test,u can start a game activity
Toast.makeText(TestGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}=======================================================================================package com.ray.test;import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
//import android.widget.ImageView;
//import android.widget.RelativeLayout.LayoutParams;
import android.widget.ImageView;public class ImageAdapter extends BaseAdapter {
private Context mContext; //define Context private Integer[] mImageIds = { //picture source
R.drawable.p1,
R.drawable.p2,
R.drawable.p3,
R.drawable.p4,
R.drawable.p5,
R.drawable.p6,
R.drawable.p7,
R.drawable.p8,
}; public ImageAdapter(Context c) { //define ImageAdapter
mContext = c;
} //get the picture number
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
// ImageView i=new ImageView(mContext);
Button button=new Button(mContext);
button.setBackgroundResource(mImageIds[position]);
// button.setLayoutParams(new Gallery.LayoutParams(192,192));
// i.setImageResource(mImageIds[position]);//set resource for the imageView
button.setLayoutParams(new Gallery.LayoutParams(100, 100));//layout
button.setPadding(10, 10, 10, 10);
//i.setScaleType(ImageView.ScaleType.FIT_XY);//set scale type
return button;
}
}理论上是可行的,但是不知道代码哪里写错了
imageview extends 自 view 可能还是内部机制不一样吧