简体   繁体   English

在Android中替代AbsoluteLayout?

[英]Alternative to AbsoluteLayout in Android?

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. 我已经完成了一个使用AbsoluteLayout的应用程序,但它不适用于不同的屏幕分辨率。 I use because I can set the X and Y position of a button. 我使用是因为我可以设置按钮的X和Y位置。 Can I set the position of a button using another layout? 我可以使用其他布局设置按钮的位置吗?

您可以使用此处所述的RelativeLayouts: 设置视图的绝对位置

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;

Consider using FrameLayout . 考虑使用FrameLayout If you set your child gravity to TOP|LEFT then you can use leftMargin and topMargin as the positions. 如果将子重力设置为TOP|LEFT则可以使用leftMargintopMargin作为位置。 As a bonus you get a few other useful positioning mechanisms by changing the gravity. 作为奖励,您可以通过改变重力获得一些其他有用的定位机制。

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. 如果你想完全控制你的观点在屏幕上的位置只是为了扩展ViewGroup并制作你自己的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. 最简单的解决方案是使用现有的布局之一,并使用边距,填充,重力等,但控件不会那么强大,并且可能会在diff设备屏幕上花费一些问题。

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. 否则,您的布局在不同设备上显示不同。

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

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