这就是我想要实现的目标
这就是“我想修改”
1 - 如何将图库中项目的背景更改为透明? 2 - 如何删除图库中每个项目周围的边框? 3 - 如何在“不被拉伸”的情况下以正常尺寸显示它们?
以下是使用的代码 的.java
public class Search extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Search.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.animals,
R.drawable.automotive,
R.drawable.directory,
R.drawable.homeoffice,
R.drawable.hobbies,
R.drawable.mother,
R.drawable.jobs,
R.drawable.realestate,
R.drawable.technology
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground,17170445 );
attr.recycle();
}
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 imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
}
答案 0 :(得分:2)
您必须调整作为图库项目创建的ImageView
的参数。在这种情况下,您必须更改getView()
函数的内容。
设置正确的背景。试试imageView.setBackgroundColor(Color.TRANSPARENT)
。它将背景设置为透明,并删除项目周围的边框(边框由背景描述)。
解决1。
您可以通过更改imageView.setScaleType()
函数中的比例类型来影响图像拉伸(或不拉伸)的方式(有关详细信息,请参阅ImageView.ScaleType的文档)。试试imageView.setScaleType(ImageView.ScaleType.FIT_CENTER)
。
您还可以设置“更好”的布局参数(尝试imageView.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.WRAP_CONTENT, Gallery.LayoutParams.WRAP_CONTENT))
)。
总结一下,getView()
函数应该是这样的:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundColor(Color.TRANSPARENT);
return imageView;
}