2011-11-04 81 views
32

我想設置Imageviews數組,ImageIcons [I]與下面的代碼中心的嚴重性,設置於機器人中心編程

ImageIcons[i] = new ImageView(this); 
ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 
layout.addView(ImageIcons[i]); 

而且我堅持了,而設置引力。我請這些人來指導我。

感謝

+0

看看我的答案[這裏](http://stackoverflow.com/questions/6101874/android-center-an-image/33039444#33039444)。它幫助我集中了Imageview。 –

回答

72

試試這個

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height); 
layoutParams.gravity=Gravity.CENTER; 
ImageIcons[i].setLayoutParams(layoutParams); 
+0

完美@Walid Hossain.Thanks很多。 –

+0

謝謝你Vivek –

3

首先做出的寬度match_parent,然後設置重力引力否則不會work.Hope它會奏效。

ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 
LayoutParams.WRAP_CONTENT)); 
ImageIcons.setGravity(Gravity.CENTER); 
+0

好吧,得到了這個。想知道出了什麼問題。感謝Android殺手。 –

8

從視圖中獲取佈局參數,對其進行修改並重新設置。

image.setBackgroundResource(R.drawable.mobile); 
LayoutParams params = (LayoutParams) image.getLayoutParams(); 
params.gravity = Gravity.CENTER; 
image.setLayoutParams(params); 
2
ImageView myImage = new ImageView(this); 
FrameLayout.LayoutParams myImageLayout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); 
myImageLayout.gravity=Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL; 
      myImage.setLayoutParams(myImageLayout); 
0
LinearLayout llBasicInfo = new LinearLayout(context); 
    llBasicInfo.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
    llBasicInfo.setOrientation(LinearLayout.VERTICAL); 
    ImageView imageView = new ImageView(context); 
    LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(200,200); 
    layoutParams.gravity=Gravity.CENTER; 
    imageView.setLayoutParams(layoutParams); 
    llImage.addView(imageView);