将视图放置在 Android 中的 FrameLayout 内
我想以编程方式在 FrameLayout 内添加一个视图,并将其放置在布局内具有特定宽度和高度的特定点.FrameLayout 支持这个吗?如果没有,我应该使用中间 ViewGroup 来实现这一点吗?
I want to add a view inside a FrameLayout programmatically and to place it in a specific point within the layout with a specific width and height. Does FrameLayout support this? If not, should I use an intermediate ViewGroup to achieve this?
int x; // Can be negative?
int y; // Can be negative?
int width;
int height;
View v = new View(context);
// v.setLayoutParams(?); // What do I put here?
frameLayout.addView(v);
我最初的想法是向 FrameLayout 添加一个 AbsoluteLayout 并将视图放置在 AbsoluteLayout 中.不幸的是,我刚刚发现 AbsoluteLayout 已被弃用.
My initial idea was to add an AbsoluteLayout to the FrameLayout and place the view inside the AbsoluteLayout. Unfortunately I just found out that AbsoluteLayout is deprecated.
任何指针将不胜感激.谢谢.
Any pointers will be much appreciated. Thanks.
使用 FrameLayout 确实所有子级都固定在屏幕的左上角,但您仍然可以控制设置它们的内边距.如果您为不同的子项设置不同的填充值,它们将显示在 FrameLayout 中的不同位置.
It's true that with FrameLayout all children are pegged to the top left of the screen, but you still have some control with setting their padding. If you set different padding values to different children, they will show up at different places in the FrameLayout.