2011-12-26 96 views
0

我做了一个自定义视图以满足我对显示数学向量的简单方法的需求。 我扩展了一个LinearLayout并为这些值添加了一个ArrayList。每次更改值时,我都会调用我的自定义方法redraw()将EditText添加到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); 
    } 
} 

回答

1

您可以通过使用((LinearLayout) findViewById(R.id.root)).removeAllViews()删除所有意见。 但是,如果您可以将新值设置为现有编辑文本视图,并且仅在您仍需要时才添加新视图,那么您为什么要重新添加所有编辑文本? 我没有完全理解你。