LinearLayout将不会显示所有内容
问题描述:
我的问题似乎很容易,但我真的无法解决它。在某个时候,我的代码在AlertDialog
内的LinearLayout
中显示了一堆图像(ImageView
)。 问题是,并非所有内容都显示出来。LinearLayout将不会显示所有内容
这里是我的代码:
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);
}
正如你所看到的,结果并不好。你有什么想法,为什么?
答
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毫安CLASSE ImageAdapter: 公共类ImageAdapter延伸BaseAdapter实现ListAdapter { 私有上下文℃; 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;
}
}
答
问题是您的LinearLayout
没有任何尺寸。宽度和高度是0.所以一切都完美。
您缺少setLayoutParams的电话,就这些。
ll.setLayoutParams(new LinearLayout.LayoutParams(FILL_PARENT, FILL_PARENT));
答
您可以使用HorizontalScrollView包裹您的LinearLayout。这样,您可以添加大量图像并滚动查看全部图像。
嗨。感谢您的帮助。我试过你的解决方案,但没有任何改变。我也试图把最低限度:布局是一个很好的高度,但图像仍然不是全部显示。完美的行为将是每种图像都有一些回车。你认为使用RelativeLayout会更好吗? – Passeur 2012-02-05 18:13:34
你可能会搜索'GridView'。 – poitroae 2012-02-05 19:11:52
我已经使用了一个TableLayout,每10张图片就有一个新的tableRow。我只关心它在大屏幕上的表现方式。 – Passeur 2012-02-07 09:39:06