将视图放置在 Android 中的 FrameLayout 内 [英] Place view inside FrameLayout in Android

查看:29
本文介绍了将视图放置在 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.

这篇关于将视图放置在 Android 中的 FrameLayout 内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆