0
我的ImageButtons的宽度有问题,它的大小不一样。我已经试验了两个小时的所有属性,没有任何东西。我在运行时创建按钮并放入行内(也是在运行时创建的)。有没有人知道这个解决方案? ImageButton的大小不一样
public static TableRow[] Create(List<Apartment> list){
TableRow[] rows=null;
try{
rows=new TableRow[list.size()*3];
int i=0;
for(final Apartment ap : list){
rows[3*i]=new TableRow(activity);
rows[3*i+1]=new TableRow(activity);
rows[3*i+2]=new TableRow(activity);
rows[3*i].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
rows[3*i+1].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
rows[3*i+2].setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT));
rows[3*i].setBackgroundColor(color_background[(i%2)]);
rows[3*i+1].setBackgroundColor(color_background[(i%2)]);
rows[3*i+2].setBackgroundColor(color_background[(i%2)]);
TextView txtMainInform=new TextView(activity);
txtMainInform.setText(ap.GetMainInformation());
txtMainInform.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rows[3*i].addView(txtMainInform);
rows[3*i].setVisibility(1);
TextView txtMoreInform=new TextView(activity);
txtMoreInform.setText(ap.GetMoreInformation());
txtMoreInform.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
rows[3*i+1].addView(txtMoreInform);
ImageButton imbCall=new ImageButton(activity);
imbCall.setImageResource(R.drawable.phone);
imbCall.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(ap.GetContact()!=null){
try {
activity.startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ap.GetContact())));
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
imbCall.setMaxWidth(24);
imbCall.setMinimumWidth(22);
ImageButton imbGallery=new ImageButton(activity);
imbGallery.setMaxWidth(24);
imbGallery.setMinimumWidth(22);
imbGallery.setImageResource(R.drawable.gallery_icon);
ImageButton imbMap=new ImageButton(activity);
imbMap.setImageResource(R.drawable.map);
imbMap.setMaxWidth(24);
imbMap.setMinimumWidth(22);
imbMap.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(activity,ResultMap.class);
activity.startActivity(i);
}
});
ImageButton imbWay=new ImageButton(activity);
imbWay.setMaxWidth(24);
imbWay.setMinimumWidth(22);
imbWay.setImageResource(R.drawable.walker);
rows[3*i+2].addView(imbCall);
rows[3*i+2].addView(imbGallery);
rows[3*i+2].addView(imbMap);
rows[3*i+2].addView(imbWay);
i++;
}
}
catch(Exception e){
}
return rows;
}
出于好奇,您在按钮中放置图像的尺寸是多少?为什么将按钮尺寸设置为WRAP_CONTENT,但是设置了最小和最大宽度,而不是将宽度设置为所需高度?另外,认真考虑用XML定义这种布局 - 它会少得多而且更加灵活,并且您可以通过DIP中的按钮而不是像素来自动支持不同尺寸的屏幕。 – 2010-11-02 00:57:39
Dimesions是24x24px。我没有为按钮设置WRAP_CONTENT和FILL_PARENT,它是用于放置按钮的行。我不知道我需要创建多少行,这就是我无法放入XML的原因。 – Damir 2010-11-02 01:13:35
您仍然可以使用xml,只需为该行创建一个xml布局。然后,您可以充气该行并根据需要多次添加。 – 2010-11-02 02:24:22