如何动态添加视图到LinearLayout?

发布于 2025-01-05 08:41:03 字数 117 浏览 3 评论 0原文

有没有办法在 android 中动态添加图像并将其定位到线性布局中?在 php 中,我可以只回显图像标签并使用 css 来定位它,不太确定如何在 android 中解决这个问题,但是因为 xml 似乎是非常严格预定义的。

Is there a way to dynamically add and position images into a linear layout in android? in php I could just echo out an image tag and use css to position it, not really sure how to tackle this in android however as the xml seems to be very rigidly predefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

初心未许 2025-01-12 08:41:03

您可以通过调用 ViewGroup.addView(View) 方法。
然而,这是一种模糊的添加视图的方式。首选的是,您首先为给定创建一个 LayoutParams布局(考虑到视图)这将允许您更密切地控制视图及其与其父视图的关系。

下面是一个将所有内容联系在一起的示例:

class MyActivity extends Activity {
    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle)

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        // Create 5 view to take up a small amount of space.

        View tmp = null;

        for (int i = 0; i < 5; i++) {
            tmp = new View(this);
            ll.addView(tmpnew LinearLayout.LayoutParams(
                50, // Width
                50);    // Height
            // Either or both the width and height may be ViewGroup.
            // (WRAP_CONTENT || FILL_PARENT || MATCH_PARENT)
        }

        setContentView(ll);
    }
}

这就是动态地将视图添加到 LinearLayout 的方法。然而,由于 LinearLayout 的结构和设计,实际上没有太多的定位方式。将 LinearLayout 视为一个在一个方向上充满视图的数组,您唯一能做的就是更改它们在数组中的位置。如果您想对视图进行精细控制,我推荐 RelativeLayout。

You can add view to any ViewGroup by calling the ViewGroup.addView(View) method.
How ever, this is kind of a vague way of adding a View. What is preferred is that you first create a LayoutParams for the given layout (with the view in mind) This will allow you to control the view and its relationship with its parent view more intimately.

Here is a sample to tie everything together:

class MyActivity extends Activity {
    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle)

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        // Create 5 view to take up a small amount of space.

        View tmp = null;

        for (int i = 0; i < 5; i++) {
            tmp = new View(this);
            ll.addView(tmpnew LinearLayout.LayoutParams(
                50, // Width
                50);    // Height
            // Either or both the width and height may be ViewGroup.
            // (WRAP_CONTENT || FILL_PARENT || MATCH_PARENT)
        }

        setContentView(ll);
    }
}

This is how you would dynamically add a view to a LinearLayout. However, due to the structure and design of a LinearLayout, there really isn't much in the way of positioning. Think of a LinearLayout as an array full of views in one direction, the only thing you can do is change where they are in the array. If you want fine control over a View, I recommend a RelativeLayout.

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