英文:
How to set a view at specific position on screen?
问题
如何在程序中将一个视图设置在屏幕上的特定点?我想使用类似于 x
和 y
的坐标点。我不想使用 translateX
或者 translateY
。例如,我想将一个视图设置在坐标点 x: 0
和 y: 0
处,这意味着它需要位于屏幕的左上角。我该如何实现?
我尝试了以下代码,但是视图仍然在 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
请注意,这里提供的是您所请求的翻译后的文本,其中只包含代码部分的翻译内容,没有其他额外的回答或信息。
英文:
How to can I set a view at a specific point on the screen programmatically? I want use a Point like x
and y
. I don't want it to translateX
or translateY
. 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. How can I achieve that?
I was trying this but the view translates X and Y what I don't want.
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
答案1
得分: 0
以下是您要翻译的内容:
安卓曾有一个布局可以支持这些要求,但我认为在性能方面表现相当糟糕,并且已经被弃用了(我希望我这样说没有错)。然而,相同的行为可以使用FrameLayout
来实现,并将视图添加到该布局中。
要在特定位置添加视图,您需要创建其布局参数以设置宽度和高度,然后设置边距,这将将您的视图定位为绝对坐标(尺寸以像素为单位):
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)
对于高度/宽度为200和边距为(100、100、0、0),它将如下图所示(在模拟的Pixel 3上运行)。
注意:这里有一个关于模拟AbsoluteLayout
的方法以及他们为什么“淘汰”它的讲座。
英文:
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). However, the same behaviour can be implemented using a FrameLayout
and add the views in that layout.
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):
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).
NOTE: Here is a talk where they talk about this approach to emulate the AbsoulteLayout
and why they "killed" it.
答案2
得分: 0
我发现这样可以工作:
ViewGroup.MarginLayoutParams margins = (ViewGroup.MarginLayoutParams) layout.getLayoutParams();
margins.topMargin = 100;
margins.leftMargin = 200;
或者可以使用动画:
view.animate().x(x).y(y).setDuration(0).start();
英文:
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();
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论