简体   繁体   中英

Handling the layout in a custom view

I made a custom view to satisfy my need for an easy way to display a mathematic vector. I extended a LinearLayout and added an ArrayList for the values. Everytime the values change I call my custom method redraw() to add EditTexts to the LinearLayout. This way after adding a value, all existing EditTexts are added once again. How to I clear the LinearLayout or display a new LinearLayout?

Here some code:

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);
    }
}

You can remove all views by using ((LinearLayout) findViewById(R.id.root)).removeAllViews() . But why do you want to add all the edit text all over again if you can set the new values to the existing edit text views and add new view only if you still need to. I didn't fully understand you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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