当前位置: 代码迷 >> Android >> 请问关于Android中setMargins不可用的有关问题
  详细解决方案

请问关于Android中setMargins不可用的有关问题

热度:16   发布时间:2016-04-28 05:45:16.0
请教关于Android中setMargins不可用的问题。
如题,请教各位,Android开发时,使用setMargin提示The method setMargins(int, int, int, int) is undefined for the type ImageView。
代码如下:

        for (int i = 0; i < pageViews.size(); i++) {  
            imageView = new ImageView(UserMainActivity.this);  
  
            imageView.setLayoutParams(new LayoutParams(7,7));  
            imageView.setPadding(7, 0, 7, 0);
            imageView.setMargins(7, 0, 7, 0);//这里不可用
  
            imageViews[i] = imageView;  
            if (i == 0) {  
                //默认选中第一张图片
                imageViews[i].setBackgroundResource(R.drawable.point_normal);  
            } else {  
                imageViews[i].setBackgroundResource(R.drawable.point_select);  
            }  
            group.addView(imageViews[i]);  
        }  

新手开发,不知道怎么解决,请各位帮帮忙,先谢谢了。
------解决方案--------------------
margin是容器的layout的属性,不是ImageView的,需要通过LayoutParams来指定
------解决方案--------------------

  imageView.setPadding(7, 0, 7, 0);
        LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(7,7);
        lp.setMargins(7, 0, 7, 0);
        imageView.setLayoutParams(lp); 
  相关解决方案