Android系统。如何在创建时移动ImageView的(固定位置/保证金) [英] Android. How to move the imageview(set position/margin) when it is created

查看:249
本文介绍了Android系统。如何在创建时移动ImageView的(固定位置/保证金)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一些图片在一个地区在应用程序启动。
由于图像的数量不是一个固定的数额,所以我必须动态地创建图像。
我想设置每个图像的位置/保证金他们良好的平衡,而创建

我试图如下,但没有效果。
·创建后,使用imageview.layout()。
·使用LayoutParams.margins()。
·使用AsyncTask的设置页边距。
·activity.runOnUiThread()。

这是code:

  //访问服务器后,如果需要,那么的ImageView添加到c_box
    // c_box:是的ImageView添加父。
    的FrameLayout c_box =(的FrameLayout)findViewById(R.id.c_box);    MarginLayoutParams MLP =新MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);    ImageView的IMG1 =新ImageView的(本);
    img1.setImageResource(R.drawable.c_file);
    img1.setLayoutParams(PARAMS);
    img1.setLongClickable(真);
    img1.setId(img_id);
    c_box.addView(IMG1);    img1.setOnTouchListener(监听);
    ** //有可能通过设置布局或保证金的位置?
    img1.layout(100,100,200,200); **

我不知道在哪里调用invalidate()方法。


  //访问服务器后,如果需要,那么的ImageView添加到c_box
    // c_box:是的ImageView添加父。
    的FrameLayout c_box =(的FrameLayout)findViewById(R.id.c_box);    MarginLayoutParams MLP =新MarginLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    mlp.setMargins(100,100,200,200);
    ImageView的IMG1 =新ImageView的(本);
    img1.setImageResource(R.drawable.c_file);
    img1.setLayoutParams(MLP);
    img1.setLongClickable(真);
    img1.setId(img_id);
    c_box.addView(IMG1);    img1.setOnTouchListener(监听);


解决方案

最后,我发现了这一点:

  FrameLayout.LayoutParams PARAMS =新FrameLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,
        Gravity.NO_GRAVITY);

是第三个参数( Gravity.NO_GRAVITY ),我错过了。使用它,你可以在设置有宽度/高度任意位置的看法。

I want to put some images in an area at the application start. As the number of images is not a fixed amount, so I have to create images dynamically. I want to set the position/margin of every images while they're created for good balance.

I have tried follows, but there is no efficacy. ・After Created, use imageview.layout(). ・Use LayoutParams.margins(). ・Use AsyncTask to set margins. ・activity.runOnUiThread().

This is the code:

    // After access server, if needed then add a imageview to the 'c_box'
    // c_box: the parent that imageview to add.
    FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);

    MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    ImageView img1 = new ImageView(this);
    img1.setImageResource(R.drawable.c_file);
    img1.setLayoutParams(params);
    img1.setLongClickable(true);
    img1.setId(img_id);
    c_box.addView(img1);

    img1.setOnTouchListener(listener);


    **// it's possible to set the position by layout or margin?
    img1.layout(100, 100, 200, 200);**

I don't know where to call the invalidate() method.


// After access server, if needed then add a imageview to the 'c_box'
    // c_box: the parent that imageview to add.
    FrameLayout c_box = (FrameLayout) findViewById(R.id.c_box);

    MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mlp.setMargins(100, 100, 200, 200);
    ImageView img1 = new ImageView(this);
    img1.setImageResource(R.drawable.c_file);
    img1.setLayoutParams(mlp);
    img1.setLongClickable(true);
    img1.setId(img_id);
    c_box.addView(img1);

    img1.setOnTouchListener(listener);

解决方案

Finally I found the point:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
        Gravity.NO_GRAVITY);

the 3rd param(Gravity.NO_GRAVITY) that I missed. use it you can position views at anywhere with setting width/height.

这篇关于Android系统。如何在创建时移动ImageView的(固定位置/保证金)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆