android:如何将xml布局中的子项添加到自定义视图中

seb*_*taz 10 android android-custom-view android-layout android-xml android-resources

在我的xml布局中,我有一个自定义视图,我将在其中放置一些像:

<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>
Run Code Online (Sandbox Code Playgroud)

让我解释一下.我写了一个自定义ScrollView,我已经为孩子们定义了一个容器.所以我只想把它们放在那里.

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 ...
}
Run Code Online (Sandbox Code Playgroud)

将Child*添加到llContainer的方法是什么?

Jin*_*n35 11

你为什么不把所有的孩子都加到LinearLayout你的孩子身上ScrollLayout?这应该在onFinishInflate()方法中完成.

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

在XML文件中编写结构时 - 所有内部视图都是自定义布局的子视图.只需将其替换为LinearLayout.


And*_*ltz 7

Jin35的答案有一个严重的问题:getChildCount()改变迭代的价值,因为我们正在删除孩子.

这应该是一个更好的解决方案:

while (getChildCount() > 0) {
    View v = getChildAt(0);
    removeViewAt(0);
    llContainer.addView(v);
}
Run Code Online (Sandbox Code Playgroud)


Fle*_*hns 5

我同意 Jin35 的回答是有缺陷的。此外,添加了 svContainer,所以我们不能继续直到 getChildCount() == 0。

到 init() 结束时,getChildCount() == 1,因为 svContainer 已添加但 TextViews 尚未添加。到 onFinishInflate() 结束时,TextViews 已经添加,应该在位置 1、2、3、4 和 5。但是如果你然后删除位置 1 的视图,其他的索引将全部减少 1(标准列表行为)。

我会建议:

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    View v;
    while ((v = getChildAt(1)) != null) {
        removeView(v);
        llContainer.addView(v);
    }
}
Run Code Online (Sandbox Code Playgroud)