我的问题似乎很容易,但我真的无法解决它。在某些时候,我的代码会在ImageView
内的LinearLayout
内显示一堆图片(AlertDialog
)。
问题是不是所有东西都显示出来。
这是我的代码:
public LinearLayout createLayout(String text) {
LinearLayout ll = new LinearLayout(this.c);
int res;
for(int i=0; i<text.length(); ++i) {
res = this.c.getResources().getIdentifier("pigpen_" + Character.toLowerCase(text.charAt(i)), "drawable", this.c.getPackageName());
ImageView iv = new ImageView(this.c);
iv.setPadding(5, 0, 0, 5);
iv.setImageResource(res);
ll.addView(iv);
}
return ll;
}
DialogAlert的代码:
protected ResultDialog(final Context context, CharSequence title, LinearLayout ll) {
super(context);
ll.setPadding(10, 5, 10, 5);
this.setView(ll);
this.setTitle(title);
}
正如您所看到的,结果并不好。你知道为什么吗?
答案 0 :(得分:0)
问题是您的LinearLayout
没有任何尺寸。宽度和高度都是0.所以一切都很完美。
你没有打电话给setLayoutParams,这就是全部。
ll.setLayoutParams(new LinearLayout.LayoutParams(FILL_PARENT, FILL_PARENT));
答案 1 :(得分:0)
您可以使用HorizontalScrollView包装LinearLayout。这样你可以添加很多图像并滚动查看它们。
答案 2 :(得分:0)
LinearLayout ll = new LinearLayout(this.c);
ArrayList<Integer> alImages = new ArrayList<Integer>();
ll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
for(int i=0; i<text.length(); ++i) {
cur = text.charAt(i);
pos = this.getCharAlphabetPosition(cur);
if(pos > 0 && pos < alphabetId.length) {
res = alphabetId[pos];
alImages.add(res);
}
}
gv.setAdapter(new ImageAdapter(this.c, alImages));
gv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
gv.setNumColumns(GridView.AUTO_FIT);
gv.setColumnWidth(25);
ll.addView(gv);
}
Et ma classe ImageAdapter: public class ImageAdapter扩展BaseAdapter实现ListAdapter { 私有语境c; private ArrayList alImages;
public ImageAdapter(Context c, ArrayList<Integer> alImages) {
this.c = c;
this.alImages = alImages;
}
public int getCount() {
return this.alImages.size();
}
public Integer getItem(int position) {
return this.alImages.get(position);
}
public long getItemId(int position) {
return 0;
}
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(this.c);
imageView.setLayoutParams(new GridView.LayoutParams(25, 25));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 0, 0, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(this.getItem(position));
return imageView;
}
}