ImageButton 大小不一样

发布于 2024-09-29 20:09:18 字数 3978 浏览 0 评论 0原文

我对 ImageButtons 的宽度有疑问,它的大小不一样。我已经尝试了两个小时的所有属性,但没有任何结果。我在运行时创建按钮并放入行内(也在运行时创建)。有人知道这个问题有什么解决办法吗? 替代文本

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;
    }

I have problem with width of ImageButtons, it isn't the same size. I have experimented with all attributes for two hours and nothing. I create buttons at runtime and put inside row (also created at runtime). Does anybody know any solution for this ?
alt text

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-10-06 20:09:18

我猜想您将这些行中的每一行添加到同一个 TableLayout 中,或者达到这种效果?如果是这样,它会将这些行与另一行关联起来,以便一行中的第一项与另一行中的第一项位于同一列中。

我几乎无法从你的图像中看出,但我的猜测是图像顶部的极度截止文本正在推动列的宽度,从而加宽第一个按钮。

|longish text blagh |       |       |       |
|price              |       |       |       |
|blah               |       |       |       |
| [------BTN------] | [BTN] | [BTN] | [BTN] |

如果这恰好是问题所在,您可以通过将按钮行放入其他类型的视图来修复它。

I would guess that you add each of those rows into the same TableLayout, or something to that effect? If so, it would associate the rows with another, so that the first item in one row is in the same column as the first item in another row.

I can barely tell by your image, but my guess would be that the extremely cut-off text at the top of the image is pushing the width of the column, widening the first button with it.

|longish text blagh |       |       |       |
|price              |       |       |       |
|blah               |       |       |       |
| [------BTN------] | [BTN] | [BTN] | [BTN] |

You could possibly fix it by putting the row of buttons into some other sort of view, if that happens to be the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文