以编程方式在android中设置ImageView的重力到中心

发布于 2024-12-14 04:17:02 字数 297 浏览 2 评论 0原文

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

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

并且在设置重力时我被卡住了。我请求 SO 人们指导我。

谢谢

I want to set the gravity of an array of Imageviews,ImageIcons[i] to the center with the following code,

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

And I am stuck up while setting the gravity.I request the SO people to guide me on this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

划一舟意中人 2024-12-21 04:17:02

试试这个

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

Try this

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(width, height);
layoutParams.gravity=Gravity.CENTER;
ImageIcons[i].setLayoutParams(layoutParams);
影子的影子 2024-12-21 04:17:02

从视图中获取布局参数,修改它并再次设置。

image.setBackgroundResource(R.drawable.mobile);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.gravity = Gravity.CENTER;
image.setLayoutParams(params);

get the layout parameters from view, modify it and set it again.

image.setBackgroundResource(R.drawable.mobile);
LayoutParams params = (LayoutParams) image.getLayoutParams();
params.gravity = Gravity.CENTER;
image.setLayoutParams(params);
写给空气的情书 2024-12-21 04:17:02

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

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

First made the width to match_parent and then set the gravity else gravity will not work.Hope it will work.

ImageIcons[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageIcons.setGravity(Gravity.CENTER);
淡看悲欢离合 2024-12-21 04:17:02
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 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);
尐偏执 2024-12-21 04:17:02
    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 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);
柳絮泡泡 2024-12-21 04:17:02

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

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

I think the code below possible helps someone

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);
别挽留 2024-12-21 04:17:02

如果您的 ImageViewRelativeLayout< 的子级/a>

然后这个可能工作有帮助..

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

if your ImageView is child of RelativeLayout

Then This May Work Helps..

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文