Android - setting X,Y of image programmatically

前端 未结 2 952
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 12:27

I have two ImageViews inside an AbsoluteLayout.



        
相关标签:
2条回答
  • 2020-12-18 12:45

    You should use:

    AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(int width, int height, int x, int y)
    

    layout Parameter object with the preferred x and y and set it to your fpLight image by

    fpLight.setLayoutParams(param);
    

    AbsoluteLayout is Deprecated

    You should use RelativeLayout instead

    EXAMPLE:

    Suppose you want a ImageView of size 50x60 on your screen at postion (70x80)

    // RelativeLayout. though you can use xml RelativeLayout here too by `findViewById()`
    RelativeLayout relativeLayout = new RelativeLayout(this);
    // ImageView
    ImageView imageView = new ImageView(this);
    
    // Setting layout params to our RelativeLayout
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 60);
    
    // Setting position of our ImageView
    layoutParams.leftMargin = 70;
    layoutParams.topMargin = 80;
    
    // Finally Adding the imageView to RelativeLayout and its position
    relativeLayout.addView(imageView, layoutParams);
    
    0 讨论(0)
  • 2020-12-18 12:45
    yourImageView.setX(floatXcoord);
    yourImageView.setY(floatYcoord);
    
    0 讨论(0)
提交回复
热议问题