如何使用图像本身和文本将图像添加到网格视图中。我尝试制作自定义图像适配器,但它不适用于某些原因在imgView.setLayoutParams上遇到崩溃(new GridView.LayoutParams(85,85));
新问题我如何添加和删除图像适配器...我需要行为像一张照片用一个和我添加相同的图片与文本ab所以应该有2个图片然后我可以删除一个用... ..
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AlbumAdapter extends BaseAdapter {
private Context mContext;
private ArrayList albNames;
public AlbumAdapter(Context c, ArrayList albNames) {
mContext = c;
this.albNames = albNames;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
/* public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 3);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
*/
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.album_image, null);
}
else {
view = convertView;
}
ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
imgView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setPadding(10, 10, 10, 3);
TextView albNameView = (TextView) view.findViewById(R.id.icon_text);
if (albNames.size() > 0) {
for (int i = 0; i < albNames.size(); i++) {
imgView.setBackgroundResource(R.drawable.folder);
albNameView.setText((String)albNames.get(i));
}
}
return view;
}
// references to our images
public Integer[] mThumbIds = { R.drawable.folder};
}
答案 0 :(得分:0)
尝试
imgView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));
修改强>
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AlbumAdapter extends BaseAdapter {
private Context mContext;
public AlbumAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
/* public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(10, 10, 10, 3);
}
else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
*/
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.album_image, null);
}
else {
view = convertView;
}
ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
imgView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imgView.setBackgroundResource(R.drawable.a);
TextView albNameView = (TextView) view.findViewById(R.id.icon_text);
albNameView.setText("as");
return view;
}
// references to our images
public Integer[] mThumbIds = { R.drawable.a };
}
答案 1 :(得分:0)
'imgView'是ConvertView的子节点,它从R.layout.album_image中膨胀,它只是一个LinearLayout。因此,在设置布局参数时,它需要是父类ViewGroup类型。所以LinearLayout.LayoutParams工作正常。
假设你想设置'ConvertView'的大小,那么在这种情况下它需要是GridView.LayoutParams,becoz'ConvertView'被添加到父GridView。
希望你明白了!! !!