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

huangapple 未分类评论54阅读模式
英文:

How to set a view at specific position on screen?

问题

如何在程序中将一个视图设置在屏幕上的特定点?我想使用类似于 xy 的坐标点。我不想使用 translateX 或者 translateY。例如,我想将一个视图设置在坐标点 x: 0y: 0 处,这意味着它需要位于屏幕的左上角。我该如何实现?

我尝试了以下代码,但是视图仍然在 X 和 Y 方向上发生了位移,而这不是我想要的效果。

  1. val layoutInflater = LayoutInflater.from(context)
  2. val inflatedView: View = layoutInflater.inflate(R.layout.fragment_change_font_size_right, null, false)
  3. inflatedView.popup_xml_view.x = 0f
  4. 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.

  1. val layoutInflater = LayoutInflater.from(context)
  2. val inflatedView: View = layoutInflater.inflate(R.layout.fragment_change_font_size_right, null, false)
  3. inflatedView.popup_xml_view.x = 0f
  4. inflatedView.popup_xml_view.y = 0f

答案1

得分: 0

以下是您要翻译的内容:

安卓曾有一个布局可以支持这些要求,但我认为在性能方面表现相当糟糕,并且已经被弃用了(我希望我这样说没有错)。然而,相同的行为可以使用FrameLayout来实现,并将视图添加到该布局中。

要在特定位置添加视图,您需要创建其布局参数以设置宽度和高度,然后设置边距,这将将您的视图定位为绝对坐标(尺寸以像素为单位):

  1. val imageView = ImageView(this)
  2. imageView.setBackgroundResource(android.R.color.holo_red_dark)
  3. imageView.layoutParams = FrameLayout.LayoutParams(width, height).apply {
  4. setMargins(left, top, right, bottom)
  5. }
  6. 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):

  1. val imageView = ImageView(this)
  2. imageView.setBackgroundResource(android.R.color.holo_red_dark)
  3. imageView.layoutParams = FrameLayout.LayoutParams(width, height).apply {
  4. setMargins(left, top, right, bottom)
  5. }
  6. 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

我发现这样可以工作:

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

或者可以使用动画:

  1. view.animate().x(x).y(y).setDuration(0).start();
英文:

I found this to work:

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

Alternatively use animate:

  1. view.animate().x(x).y(y).setDuration(0).start();
  2. </details>

huangapple
  • 本文由 发表于 2020年6月5日 22:19:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/62217493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定