机器人:如何从一个XML布局增加孩子进入自定义视图 [英] android: how to add children from an xml layout into a custom view

查看:130
本文介绍了机器人:如何从一个XML布局增加孩子进入自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的XML的布局我有一个自定义视图中,我会放一些孩子喜欢:

In my xml layouts I have a custom view in which i will put some children like:

<com.proj.layouts.components.ScrollLayout
    android:id="@+id/slBody"
    android:layout_width="700dp"
    android:layout_height="400dp">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="child1"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="child2"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="child3"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="child4"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="child5"/>
</com.proj.layouts.components.ScrollLayout>

让我解释一点。我写的,我已经有了一个容器中定义,为孩子定制的滚动型。因此,我只想把它们放在那里。

Let me explain a bit more. I wrote a custom ScrollView in which I already have a container defined, for the children. So I just want to put them there.

public class ScrollLayout extends LinearLayout {
    // View responsible for the scrolling
    private FrameLayout svContainer;
    // View holding all of the children
    private LinearLayout llContainer;

    public ScrollLayout(Context context) {
        super(context);
        init();
    }

    public ScrollLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        super.removeAllViews(); // kill old containers


        svContainer = new HorizontalScroll(getContext());
        llContainer = new LinearLayout(getContext());
        llContainer.setOrientation(orientation);
        svContainer.addView(llContainer);

        svContainer.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        llContainer.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        addView(svContainer);


    }

    ... I left out the part which takes care of the scroll event ...
}

什么是增加孩子的方式*到llContainer?

推荐答案

你为什么不只是所有的孩子加入到的LinearLayout ScrollLayout ?这应该在 onFinishInflate()方法来实现。

Why don't you just add all the children to the LinearLayout from your ScrollLayout? This should be done in the onFinishInflate() method.

for (int i = 0; i<getChildCount(); i++)
{
    View v = getChildAt(i);
    removeViewAt(i);
    llContainer.addView(v);
}

在XML文件中写你的结构 - 所有内部的看法是你的自定义布局的儿童。只要将它替换到的LinearLayout

When you write your structure in the XML file - all inner views are children of your custom layout. Just replace it to LinearLayout.

这篇关于机器人:如何从一个XML布局增加孩子进入自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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