2011-11-04 7 views
32

私はImageviewsの配列の重力を設定する、次のコードで、中心にImageIcons [i]は、アンドロイドの中央にImageViewのの重力の設定をプログラム

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

そして私はしばらく立ち往生しています重力を設定する。私はSOの人々に私にこれを導くように依頼する。

おかげ

+0

[ここ](http://stackoverflow.com/questions/6101874/android-center-an-image/33039444#33039444)私の答えをチェックしてください。私のイメージビューを一元化するのに役立った。 –

答えて

72

この

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

パーフェクト@ワリド・ホセイン。たくさんのことをありがとう。 –

+0

ありがとうございましたVivek –

3

まずmatch_parentする幅を作った後、重力、他の重力は、それが動作しますwork.Hopeません設定してください。

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

それを得ました。何がうまくいかなかったのか疑問に思っています。Android Killerに感謝します。 –

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