替代 Android 中的 AbsoluteLayout?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3539349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative to AbsoluteLayout in Android?
提问by JoeyCK
If AbsoluteLayout is deprecated what can I use instead of it?
如果 AbsoluteLayout 被弃用,我可以用什么代替它?
I've done an app that uses AbsoluteLayout but it doesn't work well with the different screen resolutions. I use because I can set the X and Y position of a button. Can I set the position of a button using another layout?
我已经完成了一个使用 AbsoluteLayout 的应用程序,但它不适用于不同的屏幕分辨率。我使用是因为我可以设置按钮的 X 和 Y 位置。我可以使用其他布局设置按钮的位置吗?
回答by Kyle
you can use RelativeLayouts as described here: Set the absolute position of a view
您可以按照此处所述使用 RelativeLayouts:设置视图的绝对位置
回答by Ganesh Kanna
Use RelativeLayout and set left and right margins like
使用 RelativeLayout 并设置左右边距,例如
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin = y;
回答by fluffy
Consider using FrameLayout. If you set your child gravity to TOP|LEFT
then you can use leftMargin
and topMargin
as the positions. As a bonus you get a few other useful positioning mechanisms by changing the gravity.
考虑使用FrameLayout。如果您将孩子的重力设置为,TOP|LEFT
那么您可以使用leftMargin
和topMargin
作为位置。作为奖励,您可以通过改变重力获得其他一些有用的定位机制。
回答by Stoycho Andreev
I will suggest you guys if you want to have full control over the positions of your views on the screen just to extend ViewGroup and make your own implementation of AbsoluteLayout. The easiest solution will be to use one of the existing Layouts and play with margins, paddings, gravity and so on, but the control is not gonna be so powerful and can cost some problems on the diff device screens.
如果你想完全控制你的视图在屏幕上的位置,我会建议你们只是为了扩展 ViewGroup 并实现你自己的 AbsoluteLayout。最简单的解决方案是使用现有布局之一并使用边距、填充、重力等,但控制不会那么强大,并且可能会在差异设备屏幕上产生一些问题。
回答by Andrew
I would just use a relative layout and set it based off of the other items/edges of the screen. Otherwise your layout appears different on different devices.
我只会使用相对布局并根据屏幕的其他项目/边缘进行设置。否则,您的布局在不同设备上会显示不同。