Android 替换线性布局中的视图

Android 替换线性布局中的视图,android,android-layout,Android,Android Layout,我希望以编程方式替换LinearLayout中的视图。我尝试了以下方法: public void drawView() { //remove previous view, if exists if(((LinearLayout) findViewById(R.id.chartView)).getChildCount() > 0) { ((LinearLayout) findViewById(R.id.chartView)).removeAllViewsInL

我希望以编程方式替换LinearLayout中的视图。我尝试了以下方法:

public void drawView() {
    //remove previous view, if exists
    if(((LinearLayout) findViewById(R.id.chartView)).getChildCount() > 0) {
        ((LinearLayout) findViewById(R.id.chartView)).removeAllViewsInLayout();
    }

    //generate new view
    CustomView view = new CustomView(this,parameters);

    //add new view to layout
    ((LinearLayout) findViewById(R.id.linLayout)).addView(view);
}
此方法引用的LinearLayout在XML中定义:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linLayout">
</LinearLayout>


第一次正确绘制视图(当LinearLayout中还没有视图时。但是第二次调用drawView时,删除了以前的视图,但没有添加新视图。如何以编程方式替换此编程生成的视图?

您正在从此布局中删除
((LinearLayout)findViewById(R.id.chartView))。移除AllViewsInLayout();
但还要添加:
((LinearLayout)findViewById(R.id.linLayout)).addView(view);

R.id.linLayout
替换为
R.id.chartView