android layout_weight创造了差距

时间:2015-04-19 18:54:54

标签: android

我是android开发新手。 我尝试迭代Hashmap来创建垂直线性布局,它总是包含一个ImageView和一个Button。 这到目前为止工作正常。要分配等于所有线性布局的可用空间,我尝试使用weight属性。这也有效。但不幸的是,它在图像和按钮之间产生了一个巨大的间隙。我不明白为什么。有什么想法吗?

Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            if(count%3==0){
                if(linear!=null){
                    layout.addView(linear);
                }
                linear=new LinearLayout(getApplicationContext());
                linear.setOrientation(LinearLayout.HORIZONTAL);
                linear.setLayoutParams(layoutParams);
            }
            LinearLayout linear2=new LinearLayout(getApplicationContext());
            LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0, 
                    LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
            lp1.setMargins(15, 0, 15, 0);
            linear2.setLayoutParams(lp1);
            linear2.setOrientation(LinearLayout.VERTICAL);
            final String finalstring=(String)pair.getKey();
            Button button = new Button(getApplicationContext());
            button.setText((String)pair.getKey());
            button.setClickable(false);
            button.setBackgroundColor(getResources().getColor(R.color.friking_blue));

            ImageView image = new ImageView(getApplicationContext());
            new DownloadImageTask(image).execute((String)pair.getValue());

            linear2.addView(image);
            linear2.addView(button);
            linear.addView(linear2);
            linear2.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                    Intent intent = new Intent(getApplicationContext(), Mycontacts.class);
                    intent.putExtra("category", finalstring);
                    startActivity(intent);
                }
            });
            count++;
            it.remove(); // avoids a ConcurrentModificationException
        }

这是图像:

Image of the problem

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您需要在scaleType中指定ImageView。 创建ImageView后,请致电:

 image.setScaleType(ImageView.ScaleType.CENTER_CROP);

或其他一些scaleType,以便填满整个视图。