在 Android 中将视图放置在 FrameLayout 中

发布于 2024-09-26 05:31:45 字数 438 浏览 9 评论 0原文

我想以编程方式在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剩余の解释 2024-10-03 05:31:45

以下示例(工作代码)显示如何将视图 (EditText) 放置在 FrameLayout 内。它还演示了如何使用 FrameLayout 的 setPadding setter 设置 EditText 的位置(每次用户单击 FrameLayout 时,EditText 的位置都会设置为单击的位置):

public class TextToolTestActivity extends Activity{
    FrameLayout frmLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        frmLayout = (FrameLayout)findViewById(R.id.frameLayout1);
        frmLayout.setFocusable(true);
        EditText et = new EditText(this);

        frmLayout.addView(et,100,100);
        frmLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("TESTING","touch x,y == " + event.getX() + "," +     event.getY() );
                frmLayout.setPadding(Math.round(event.getX()),Math.round(event.getY()) , 0, 0);
            return true;
        }
    });

}

}

main.xml < /b>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout 
        android:id="@+id/frameLayout1" 
        android:layout_height="fill_parent" android:layout_width="fill_parent">
    </FrameLayout>

</LinearLayout>

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):

public class TextToolTestActivity extends Activity{
    FrameLayout frmLayout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        frmLayout = (FrameLayout)findViewById(R.id.frameLayout1);
        frmLayout.setFocusable(true);
        EditText et = new EditText(this);

        frmLayout.addView(et,100,100);
        frmLayout.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("TESTING","touch x,y == " + event.getX() + "," +     event.getY() );
                frmLayout.setPadding(Math.round(event.getX()),Math.round(event.getY()) , 0, 0);
            return true;
        }
    });

}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout 
        android:id="@+id/frameLayout1" 
        android:layout_height="fill_parent" android:layout_width="fill_parent">
    </FrameLayout>

</LinearLayout>
小鸟爱天空丶 2024-10-03 05:31:45

您还可以在新添加的视图周围添加边距,以将其放置在 FrameLayout 内。

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main); // or some other R.id.xxx
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, metrics.heightPixels - 20, 0, 0);
View v = new View(context);
v.setLayoutParams(params);
frameLayout.addView(v);

这会将 FrameLayout 定位在距屏幕底部 20 像素的位置。

编辑:完成了示例,因此它可以独立存在。哦,是的,它确实有效。

You can also add a margin around the newly added view to position it inside the FrameLayout.

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.main); // or some other R.id.xxx
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, metrics.heightPixels - 20, 0, 0);
View v = new View(context);
v.setLayoutParams(params);
frameLayout.addView(v);

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.

围归者 2024-10-03 05:31:45

确实,使用 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.

雨轻弹 2024-10-03 05:31:45

从 Quinn1000 提供的 链接 中:

您可以向 FrameLayout 添加多个子项,但所有子项都固定在屏幕的左上角。

这意味着您不能将 View 放置在 FrameLayout 内的特定点(除非您希望它位于左上角:-))。

如果您需要视图的绝对定位,请尝试 AbsoluteLayout

允许您指定其子级的确切位置(x/y 坐标)的布局。与没有绝对定位的其他类型的布局相比,绝对布局不太灵活且更难维护。

至于设置 View 的宽度和高度,也像 Quinn1000 所说的那样,您提供 v.setLayoutParams() 方法 LayoutParams 对象,具体取决于您选择的容器(AbsoluteLayout、LinearLayout 等)

From the link Quinn1000 provided:

You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen.

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:

A layout that lets you specify exact locations (x/y coordinates) of its children. Absolute layouts are less flexible and harder to maintain than other types of layouts without absolute positioning.

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.)

玩世 2024-10-03 05:31:45

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文