2011-12-26 74 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()刪除所有意見。 但是,如果您可以將新值設置爲現有編輯文本視圖,並且僅在您仍需要時才添加新視圖,那麼您爲什麼要重新添加所有編輯文本? 我沒有完全理解你。