问题
This problem might seem very basic but I couldn't find an answer to it. I need to place an imageView in a special position in the screen. Suppose, I need it to be three tenth from the top, and one fifth from the left of the screen.
The first thing that comes to mind is to use an AbsoluteLayout and place the imageView using absolute coordinates. However this method causes problems in working with different screen sizes. The problem is that I can not get the screen dimensions (width and height) in the xml file, and on the other hand, I couldn't find a setX() / setY() or similar method for ImageView to do it programatically.
An idea to solve this problem was using LinearLayout instead of AbsoluteLayout and to insert dummy views (like textVies) to fill the space behind or above my desired imageView. But this gets complicated if I have multiple imageViews to place in the screen and if the desired positions are not aligned. Another solution was using padding or margin, but it didn't work with the AbsoluteView.
回答1:
You can easily position one or more views in your screen, using LinearLayout and RelativeLayout, at any position you desire.
while using LinearLayout, use its properties like gravity and weight in corresponding xml to adjust your views.
while using RelativeLayout you can use align, margin and XXTo where XX maybe left, right, top, below of any existing views.
If did't got your answer please be specific where and how many views you want to place.
回答2:
It was like this:
AbsoluteLayout.LayoutParams lp=new AbsoluteLayout.LayoutParams(width,height,x,y);
myImageView.setLayoutParams(lp);
来源:https://stackoverflow.com/questions/14982328/posision-a-view-imageview-programatically-in-android