簡體   English   中英

將imageview添加到特定坐標的布局

[英]Add imageview to layout on specific coordinates

我正在動態地將ImageViews添加到布局中。 這是代碼,它正在做我想要的:

    setContentView(R.layout.main);
    RelativeLayout main = (RelativeLayout) findViewById(R.id.main_view);
    ImageView smokeImage = new ImageView(this);
    Drawable smoke = getResources().getDrawable(R.drawable.smoke);
    smokeImage.setBackgroundDrawable(smoke);
    main.addView(smokeImage, 800, 800);

如何將ImageView添加到特定坐標? 例如。 我希望它出現在x = 25px和y = 43px上。 有什么特殊功能嗎?

最簡單的方法是為圖像視圖設置邊距。 您可以使用create RelativeLayout.LayoutParams ,將邊距設置為params

params.setMargins(left, top, right, bottom);

並為您的圖像視圖設置參數

smokeImage.setLayoutParams(params);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM