【问题标题】:Setting margins for an imageview dynamically动态设置图像视图的边距
【发布时间】:2011-10-28 10:46:10
【问题描述】:

我可以知道如何在 imageview 中动态设置边距吗?

【问题讨论】:

标签: android android-imageview margins


【解决方案1】:

动态创建布局并将其参数设置为 setmargin() 不会直接在 imageView 上工作

ImageView im;
im = (ImageView) findViewById(R.id.your_image_in_XML_by_id);
 RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(im.getLayoutParams());
                        layout.setMargins(counter*27, 0, 0, 0);//left,right,top,bottom
                        im.setLayoutParams(layout);
                        im.setImageResource(R.drawable.yourimage)

【讨论】:

    【解决方案2】:

    您可能正在寻找这样的东西:http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)

    请注意这部分方法描述:

    这些为这个视图的父级提供参数,指定它如何 应该安排的

    这意味着如果您在 LinearLayout 中有一个 ImageView,则需要为该方法提供 LinearLayout.LayoutParams,如下所示:

    ImageView image = new ImageView(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);
    params.setMargins(1, 1, 1, 1);
    image.setLayoutParams(params);
    

    然后您只需调用 setMargins 或设置 LayoutParams 的特定 leftMargin、bottomMargin 等属性。

    【讨论】:

    • 如何设置相对布局的边距?
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多