简体   繁体   English

如何在屏幕上的特定 position 处设置视图?

[英]How to set a view at specific position on screen?

How to can I set a view at a specific point on the screen programmatically?如何以编程方式在屏幕上的特定点设置视图? I want use a Point like x and y .我想使用像xy这样的点。 I don't want it to translateX or translateY .不想让它translateXtranslateY I want for example to set a view to the Point x: 0 and y: 0 that means it needs to be at the top left of the screen.例如,我想为 Point x: 0y: 0设置一个视图,这意味着它需要位于屏幕的左上角。 How can I achieve that?我怎样才能做到这一点?

I was trying this but the view translates X and Y what I don't want.我正在尝试这个,但视图翻译了我不想要的 X 和 Y。

val layoutInflater = LayoutInflater.from(context)

val inflatedView: View = layoutInflater.inflate(R.layout.fragment_change_font_size_right, null, false)

inflatedView.popup_xml_view.x = 0f
inflatedView.popup_xml_view.y = 0f

Android had a layout that would support this requirements, but I think that it was pretty bad in regards to performance and it was deprecated (I hope that I am not wrong making this affirmation). Android 的布局可以支持此要求,但我认为它在性能方面非常糟糕并且已被弃用(我希望我做出这个肯定没有错)。 However, the same behaviour can be implemented using a FrameLayout and add the views in that layout.但是,可以使用FrameLayout实现相同的行为并在该布局中添加视图。

To add a view at a particular position, you create its layout params to set a width and a height, then set the margins which will position your view as absolute coordinates (the dimension is in pixels):要在特定的 position 添加视图,请创建其布局参数以设置宽度和高度,然后将 position 的边距设置为绝对坐标(尺寸以像素为单位):

val imageView = ImageView(this)
imageView.setBackgroundResource(android.R.color.holo_red_dark)
imageView.layoutParams = FrameLayout.LayoutParams(width, height).apply {
    setMargins(left, top, right, bottom)
}
root.addView(imageView)

For a height/width of 200 and margins of (100, 100, 0, 0), it will look something like the image below (run on an emulated Pixel 3).对于 200 的高度/宽度和 (100, 100, 0, 0) 的边距,它将类似于下图(在模拟的 Pixel 3 上运行)。

截屏

NOTE: Here is a talk where they talk about this approach to emulate the AbsoulteLayout and why they "killed" it.注意:是一个谈话,他们讨论了这种模拟AbsoulteLayout的方法以及他们“杀死”它的原因。

I found this to work:我发现这个工作:

ViewGroup.MarginLayoutParams margins = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
margins.topMargin = 100;
margins.leftMargin = 200;

Alternatively use animate:或者使用动画:

view.animate().x(x).y(y).setDuration(0).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式将视图高度和位置设置为屏幕尺寸的一部分 - Programmatically set view height and position to fraction of screen size 如何删除View分页器的特定位置 - How to delete specific position of View pager 如何通过命令行设置特定位置? - how to set a specific position though command line? 如何在长的特定位置设置/取消位? - How to set/unset a bit at specific position of a long? 如何基于其他视图位置设置视图旋转 - How to set rotation of a view based on other view position 如何根据另一个视图的位置设置视图宽度? - How to set view width according to another view's position? 如何在JavaFX中将轴(三重轴)设置在屏幕上的固定位置? - How to set axis (triad) at fixed position on screen in JavaFX? 如何使用位置访问网格视图中的特定网格项 - How to Access a specific Grid Item in a Grid View using position 如何将视图显示放到屏幕上的随机位置(适应不同的屏幕尺寸) - How can I get a view display into random position in the screen (adaptable to different screen sizes) 如何按位置设置列表视图中特定项目的背景颜色? - How to set the background color of a specific item in listview by position?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM