次のコードを使用して、Imageviews、ImageIcons [i]の配列の重力を中心に設定します。
ImageIcons[i] = new ImageView(this);
ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
layout.addView(ImageIcons[i]);
そして、重力の設定中に立ち往生しています。SO人々にこれを案内してくれるようお願いします。
ありがとう
これを試して
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height);
layoutParams.gravity=Gravity.CENTER;
ImageIcons[i].setLayoutParams(layoutParams);
ビューからレイアウトパラメータを取得し、変更して再設定します。
image.setBackgroundResource(R.drawable.mobile);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.gravity = Gravity.CENTER;
image.setLayoutParams(params);
最初に幅をmatch_parentに設定し、次に重力を設定します。それ以外の場合、重力は機能しません。
ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageIcons.setGravity(Gravity.CENTER);
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);
ImageView
が RelativeLayout
の子である場合
それからこれは役立ちます。
public void setLogoPosition(String pos)
{
//_Watermark is ImageView Object
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) _Watermark.getLayoutParams();
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
switch (pos) {
case "topleft":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
break;
case "topright":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
break;
case "bottomleft":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
break;
case "bottomright":
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
break;
case "center":
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
break;
case "topcenter":
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
break;
case "bottomcenter":
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
break;
}
_Watermark.setLayoutParams(layoutParams);
}
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);
以下のコードが誰かに役立つと思う
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = 10;
layoutParams.rightMargin = 10;
parent.addView(tips[i], layoutParams);