Android: placing an ImageView on an exact location on the screen

坚强是说给别人听的谎言 提交于 2019-12-02 12:55:45

The javadoc for view says that setX and setY will offsett the image from it's original location. It looks like what you want to use is setLeft and setTop.

https://developer.android.com/reference/android/view/View#setleft

If i have overlapping views I generally put them in layout and show/hide them. However if you want to dit via code try setting layout params of second image like:

lp.addRule(RelativeLayout.ALIGN_LEFT, image1.getId());

lp.addRule(RelativeLayout.ALIGN_TOP, image1.getId());

...something like it. Positioning depends a lot on parent of Image views. Relative Layout would be correct choice.

Step #1: Put imgv1 in a FrameLayout

Step #2: Put imgv2 in that same FrameLayout, with android:visibility="gone"

Step #3: When the user presses the button, call imgv2.setVisibility(View.VISIBLE)

<FrameLayout android:id="combined">
  <ImageView android:id="imgv1" android:layout_width="match_parent" android:layout_height="match_parent" />
  <ImageView android:id="imgv2" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent"  />
</FrameLayout>

Missing are sizing/positioning rules for the FrameLayout, which would be whatever you are presently using for your starting conditions for imgv1, presumably.


Alternatively, have one ImageView, rather than two, and change the image on the button click. For example, you could use a LayerDrawable (or the equivalent resource) to layer two drawables on top of each other, and show that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!