43

我想使用以下代码将 Imageviews,ImageIcons[i] 数组的重力设置为中心,

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

我在设置重力时被卡住了。我要求 SO 的人在这方面指导我。

谢谢

4

7 回答 7

103

尝试这个

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height);
layoutParams.gravity=Gravity.CENTER;
ImageIcons[i].setLayoutParams(layoutParams);
于 2011-11-04T06:24:03.913 回答
12

从视图中获取布局参数,修改并重新设置。

image.setBackgroundResource(R.drawable.mobile);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.gravity = Gravity.CENTER;
image.setLayoutParams(params);
于 2014-12-02T14:16:38.150 回答
4

首先将宽度设置为 match_parent,然后设置重力,否则重力将不起作用。希望它会起作用。

ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageIcons.setGravity(Gravity.CENTER);
于 2011-11-04T06:40:55.930 回答
3
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);
于 2016-05-08T05:57:56.267 回答
0

如果你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);
}
于 2019-03-16T13:16:28.693 回答
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);
于 2016-11-26T05:34:00.653 回答
0

我认为下面的代码可能对某人有帮助

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);
于 2019-03-15T07:15:02.743 回答