我有两个片段。在第一个中有两个按钮,我希望根据单击的按钮,它会在右侧的片段中加载一个或另一个布局。
在活动中一个按钮的 onclick 之一中,我有这个:
FrameLayout contenedor = (FrameLayout) findViewById(R.id.fragmento_derecha);
LayoutInflater inflater=LayoutInflater.from(this);
View viewinflado=inflater.inflate(R.layout.boton1,contenedor,true);
在另一个按钮上:
FrameLayout contenedor = (FrameLayout) findViewById(R.id.fragmento_derecha);
LayoutInflater inflater=LayoutInflater.from(this);
View viewinflado=inflater.inflate(R.layout.boton2,contenedor,true);
但是,这并不能满足我的要求。单击按钮时它会很好地膨胀一个或另一个布局,但它不会删除前一个布局的元素。我该如何解决?
您必须执行FragmentTransaction ,才能在FrameLayout中加载Fragment或其他:
例如,要加载包含布局的 Fragment
R.layout.boton1
:或包含布局的片段
R.layout.boton2
:这些将是
miFragmentBoton1()
它膨胀的类R.layout.boton1
:并且
miFragmentBoton2()
膨胀R.layout.boton2
: