Android 布局在运行时用另一个视图替换一个视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3334048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:47:46  来源:igfitidea点击:

Android layout replacing a view with another view on run time

androidandroid-layout

提问by Christian

I have a xml-layout file main with two textviews A/B and a view C. I have two other xml-layout files option1and option2. Is it possible to load either option1or option2in run time via Java into C? If so, what function do I have to use?

我有一个xml-layout 文件 main,其中包含两个xml文本视图 A/B 和一个视图 C。我还有另外两个-layout 文件option1option2. 是否可以通过 Java加载option1option2在运行时加载到 C 中?如果是这样,我必须使用什么功能?

回答by broot

You could replace any view at any time.

您可以随时替换任何视图。

int optionId = someExpression ? R.layout.option1 : R.layout.option2;

View C = findViewById(R.id.C);
ViewGroup parent = (ViewGroup) C.getParent();
int index = parent.indexOfChild(C);
parent.removeView(C);
C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);

If you don't want to replace already existing View, but choose between option1/option2 at initialization time, then you could do this easier: set android:idfor parent layout and then:

如果您不想替换已经存在的View,而是在初始化时在 option1/option2 之间进行选择,那么您可以更轻松地做到这一点:设置android:id父布局,然后:

ViewGroup parent = (ViewGroup) findViewById(R.id.parent);
View C = getLayoutInflater().inflate(optionId, parent, false);
parent.addView(C, index);

You will have to set "index" to proper value depending on views structure. You could also use a ViewStub: add your C view as ViewStub and then:

您必须根据视图结构将“索引”设置为适当的值。您还可以使用ViewStub:将您的 C 视图添加为 ViewStub 然后:

ViewStub C = (ViewStub) findViewById(R.id.C);
C.setLayoutResource(optionId);
C.inflate();

That way you won't have to worry about above "index" value if you will want to restructure your XML layout.

这样,如果您想重构 XML 布局,就不必担心上面的“索引”值。

回答by Snicolas

And if you do that very often, you could use a ViewSwitcheror a ViewFlipperto ease view substitution.

如果你经常这样做,你可以使用ViewSwitcherViewFlipper来简化视图替换。

回答by Kaiwalya Rangle

private void replaceView(View oldV,View newV){
        ViewGroup par = (ViewGroup)oldV.getParent();
        if(par == null){return;}
        int i1 = par.indexOfChild(oldV);
        par.removeViewAt(i1);
        par.addView(newV,i1);
    }

回答by ch13mob

it work in my case, oldSensor and newSnsor - oldView and newView:

它适用于我的情况,oldSensor 和 newSnsor - oldView 和 newView:

private void replaceSensors(View oldSensor, View newSensor) {
            ViewGroup parent = (ViewGroup) oldSensor.getParent();

            if (parent == null) {
                return;
            }

            int indexOldSensor = parent.indexOfChild(oldSensor);
            int indexNewSensor = parent.indexOfChild(newSensor);
            parent.removeView(oldSensor);
            parent.addView(oldSensor, indexNewSensor);
            parent.removeView(newSensor);
            parent.addView(newSensor, indexOldSensor);
        }