How can I change the position of view through code? Like changing its X, Y position. Is it possible?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Use
RelativeLayout
, place your view in it, getRelativeLayout.LayoutParams
object from your view and set margins as you need. Then callrequestLayout()
on your view. This is the only way I know.I would recommend using
setTranslationX
andsetTranslationY
. I'm only just getting started on this myself, but these seem to be the safest and preferred way of moving a view. I guess it depends a lot on what exactly you're trying to do, but this is working well for me for 2D animation.Set the left position of this view relative to its parent:
Set the right position of this view relative to its parent:
Set the top position of this view relative to its parent:
Set the bottom position of this view relative to its parent:
The above methods are used to set the position the view related to its parent.
Use LayoutParams. If you are using a LinearLayout you have to import android.widget.LinearLayout.LayoutParams, else import the proper version of LayoutParams for the layout you're using, or it will cause a ClassCastException, then:
NB: Note that you can use also imageView.setLeft(int dim), BUT THIS WON'T set the position of the component, it will set only the position of the left border of the component, the rest will remain at the same position.
There are different valid answers already, but none seems to properly suggest which method(s) to use in which case, except for the corresponding API level restrictions:
If you can wait for a layout cycle and the parent view group supports
MarginLayoutParams
(or a subclass), setmarginLeft
/marginTop
accordingly.If you need to change the position immediately and persistently (e.g. for a PopupMenu anchor), additionally call
layout(l, t, r, b)
with the same coordinates. This preempts what the layout system will confirm later.For immediate (temporary) changes (such as animations), use
setX()
/setY()
instead. In cases where the parent size doesn't depend on WRAP_CHILDREN, it might be fine to usesetX()
/setY()
exclusively.Never use
setLeft()
/setRight()
/setBottom()
/setTop()
, see below.Background: The
mLeft
/mTop
/mBottom
/mRight
fields get filled from the corresponding LayoutParams in layout(). Layout is called implicitly and asynchronously by the Android view layout system. Thus, setting theMarginLayoutParams
seems to be the safest and cleanest way to set the position permanently. However, the asynchronous layout lag might be a problem in some cases, e.g. when using a View to render a cursor, and it's supposed to be re-positioned and serve as a PopupMenu anchor at the same time. In this case, callinglayout()
worked fine for me.The problems with
setLeft()
andsetTop()
are:Calling them alone is not sufficient -- you also need to call
setRight()
andsetBottom()
to avoid stretching or shrinking the view.The implementation of these methods looks relatively complex (= doing some work to account for the view size changes caused by each of them)
They seem to cause strange issues with input fields: EditText soft numeric keyboard sometimes does not allow digits
setX()
andsetY()
work outside of the layout system, and the corresponding values are treated as an additional offset to the left / top / bottom / right values determined by the layout system, shifting the view accordingly. They seem to have been added for animations (where an immediate effect without going through a layout cycle is required).