在 Android 中将视图放置在 FrameLayout 中
我想以编程方式在 FrameLayout 内添加视图,并将其放置在具有特定宽度和高度的布局内的特定点中。 FrameLayout支持这个吗?如果不是,我应该使用中间 ViewGroup 来实现此目的吗?
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 已被弃用。
任何指示将不胜感激。谢谢。
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下示例(工作代码)显示如何将视图 (EditText) 放置在 FrameLayout 内。它还演示了如何使用 FrameLayout 的 setPadding setter 设置 EditText 的位置(每次用户单击 FrameLayout 时,EditText 的位置都会设置为单击的位置):
}
main.xml < /b>
The following example (working code) shows how to place a view (EditText) inside of a FrameLayout. Also it shows how to set the position of the EditText using the setPadding setter of the FrameLayout (everytime the user clicks on the FrameLayout, the position of the EditText is set to the position of the click):
}
main.xml
您还可以在新添加的视图周围添加边距,以将其放置在 FrameLayout 内。
这会将 FrameLayout 定位在距屏幕底部 20 像素的位置。
编辑:完成了示例,因此它可以独立存在。哦,是的,它确实有效。
You can also add a margin around the newly added view to position it inside the FrameLayout.
This will position the FrameLayout 20 pixels from the bottom of the screen.
Edit: completed the example so it stands by itself. And oh, yes it does work.
确实,使用 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.
从 Quinn1000 提供的 链接 中:
这意味着您不能将 View 放置在 FrameLayout 内的特定点(除非您希望它位于左上角:-))。
如果您需要视图的绝对定位,请尝试 AbsoluteLayout:
至于设置 View 的宽度和高度,也像 Quinn1000 所说的那样,您提供
v.setLayoutParams()
方法 LayoutParams 对象,具体取决于您选择的容器(AbsoluteLayout、LinearLayout 等)From the link Quinn1000 provided:
This means you can't put your View at a specific point inside the FrameLayout (except you want it to be at the top left corner :-)).
If you need the absolute positioning of the View, try the AbsoluteLayout:
As for setting the width and height of the View, also like Quinn1000 said, you supply the
v.setLayoutParams()
method a LayoutParams object, depending on the container you chose (AbsoluteLayout, LinearLayout, etc.)stackOverflow 上的线程位于
How do you setLayoutParams() for an ImageView?
稍微涵盖了它。
例如:
LinearLayout.LayoutParamslayoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);
意味着您需要定义一个 LinearLayout.LayoutParams (或者在您的情况下为 FrameLayout.layoutParams)对象以传递给 v 对象的 setLayoutParams 方法。
在
http://developer.android.com/reference/android/widget/FrameLayout .html
它几乎让你看起来像你可以要求你的 v:
generateDefaultLayoutParams () 通过这个方法,如果你没有具体定义参数。
但已经晚了,这些链接让我的眼睛有点流血。如果他们有帮助,请告诉我:-)
The thread here on stackOverflow at
How do you setLayoutParams() for an ImageView?
covers it somewhat.
For instance:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
yourImageView.setLayoutParams(layoutParams);
implies that you need to be defining a LinearLayout.LayoutParams (or in your case a FrameLayout.layoutParams) object to pass to the setLayoutParams method of your v object.
At
http://developer.android.com/reference/android/widget/FrameLayout.html
it almost makes it looks like you could ask your v to:
generateDefaultLayoutParams () via this method if you have not defined the parameters specifically.
But it's late, and those links are making my eyes bleed a little. Let me know if they nhelp any :-)