簡體   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