简体   繁体   English

Android:AbsoluteLayout的替代品(我确实需要绝对定位)

[英]Android: Alternative to AbsoluteLayout (I really do need absolute positioning)

I am trying to write a calendar app and I plan to have a grid (week view) which will probably be a TableLayout and directly on top of that I will have to absolutely position events on the grid. 我正在尝试编写一个日历应用程序,我计划有一个网格(周视图),这可能是一个TableLayout ,直接在那之上,我必须绝对在网格上定位事件。

But AbsoluteLayout is deprecated. AbsoluteLayout已被弃用。 What should I use instead? 我应该用什么呢?

Events may overlap and I think it would be really silly to try and use a non absolute layout to achieve what I want. 事件可能重叠,我认为尝试使用非绝对布局来实现我想要的东西真的很傻。

Maybe I should use RelativeLayout with a margin left and margin top on each of the child nodes. 也许我应该使用RelativeLayout ,每个子节点都有一个左边距和边距顶部。 Seems odd to do it that way and might not be as efficient. 看起来很奇怪这样做,可能效率不高。 Is this the best way or is there an alternative? 这是最好的方式还是有替代方案?

But AbsoluteLayout is deprecated. 但AbsoluteLayout已被弃用。 What should I use instead? 我应该用什么呢?

Write your own layout manager that implements the rules you want. 编写自己的布局管理器,实现所需的规则。

  • CommonsWare suggested writing my own layout manager and Christian pointed out it sounds easier than it is. CommonsWare建议编写我自己的布局管理器,Christian指出它听起来比现在容易。
  • Yahel suggested SurfaceView/OpenGl and draw whatever I want. Yahel建议使用SurfaceView / OpenGl并绘制我想要的任何内容。

I found out you can recreate absolute positioning by adding your child views to a RelativeLayout and set the RelativeLayout.LayoutParams to have only default values except width, height, marginTop and marginLeft. 我发现,您可以通过添加你的孩子的意见,以重建一个绝对定位RelativeLayout并设置RelativeLayout.LayoutParams有效,但宽度,高度,marginTop和marginLeft只默认值。 The top and left margin will be similar to top and left in AbsoluteLayout . AbsoluteLayout边距和左边距类似于顶部和左边。 Also, negative margins are supported. 此外,支持负边距。

Make sure you account for screen density and width and orientation changes and all the other caveats of absolute positioning that used to apply to AbsoluteLayout 确保考虑屏幕密度和宽度和方向的变化以及用于AbsoluteLayout的绝对定位的所有其他警告

If you have problems with getting your content to overflow past the right edge of the screen, try supplementing your positive left margin with an equally negative right margin. 如果您在将内容溢出屏幕右边缘时遇到问题,请尝试使用同样负的右边距补充正左边距。 (original question) (原始问题)

I would suggest either going the easy way : 我建议要么采取简单的方法:

  1. Setting your calendar for 15(or 30) minutes intervals, this way you don't need absolute positionning. 将日历设置为15(或30)分钟间隔,这样您就不需要绝对定位。 A table view filled with linear view each representing 15 minutes interval and fill these with events. 填充线性视图的表视图,每个线性视图代表15分钟的间隔,并用事件填充这些视图

Or going the hard but a lot more stable/speedy/customisable way : 或者努力但更稳定/快速/可定制的方式:

  1. SurfaceView/OpenGl draw your own however you want. 无论你想要什么,SurfaceView / OpenGl都可以自己绘制。

The problem with number 1 is the fact that the more you add elements in your view hierachy the more your app will take a performance hit. 数字1的问题在于,您在视图中添加元素的次数越多,您的应用就越会受到性能影响。 Say a conventional month you have 3 appointments a day, your hierarchy will be filled with a hundred views wich will be very long to render and heavy memory-wise too. 假设一个常规的月份,你每天有3个约会,你的层次结构将被填充一百个视图,这将是非常长的渲染和沉重的记忆。

The problem with number 2 : Well it's a lot harder to code at first. 第2个问题:首先编写代码要困难得多。 If you have to write your own layoutmanager, don't, go surfaceview or openGL. 如果您必须编写自己的布局管理器,请不要使用surfaceview或openGL。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM