繁体   English   中英

在自定义视图中处理布局

[英]Handling the layout in a custom view

我制作了一个自定义视图,以满足我对显示数学向量的简单方法的需求。 我扩展了LinearLayout并为这些值添加了ArrayList。 每次值更改时,我都会调用自定义方法redraw()以将EditTexts添加到LinearLayout。 这样,在添加值之后,将再次添加所有现有的EditText。 如何清除LinearLayout或显示新的LinearLayout?

这里有一些代码:

public Vector(Context context, AttributeSet attrs) {
    super(context, attrs);
    setWillNotDraw(false);
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (inflater != null) {
        inflater.inflate(R.layout.vector, this);
    }
}

public void redraw() {
    for (Float value : getArray()) {
        EditText edit = new EditText(getContext());
        edit.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.FILL_PARENT));
        edit.setText(value.toString());

        ((LinearLayout) findViewById(R.id.root)).addView(edit);
    }
}

您可以使用((LinearLayout) findViewById(R.id.root)).removeAllViews()删除所有视图。 但是,如果可以将新值设置为现有的编辑文本视图,并仅在仍然需要时添加新视图,为什么还要重新添加所有编辑文本。 我不完全了解你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM