fragmento não está substituindo propriamente na mudança de LANDSCAPE e PORTTAINT
-
21-12-2019 - |
Pergunta
eu quero mudar o layout do fragmento quando a configuração mudar usando o fragmento ..mas o fragmento está sobreposto no layout da atividade, então me ajude como resolver isso ...e este arquivo de classe .........
public class Self_funded_1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Self_fragment ls_fragment = new Self_fragment();
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.self_funded_category);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
/**
* Landscape mode of the device
*/
Log.i("LM", "LM");
ls_fragment = new Self_fragment();
fragmentTransaction.replace(android.R.id.content, ls_fragment);
} else {
/**
* Portrait mode of the device
*/
Log.i("LM", "PM");
PM_self_funded pm_fragment = new PM_self_funded();
fragmentTransaction.replace(android.R.id.content, pm_fragment);
fragmentTransaction.remove(ls_fragment);
fragmentTransaction.hide(ls_fragment);
}
fragmentTransaction.commit();
}
este é o arquivo xml para atividade.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/lm_fragment"
android:name="info.com.forms.Self_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/pm_fragment"
android:name="info.com.forms.PM_self_funded"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
isso é instantâneo........
Solução
Você está declarando os fragmentos no xml e também programaticamente na atividade.Você precisa apenas escolher uma opção.
Por exemplo, crie dois arquivos de layout diferentes, um para retrato e outro para paisagem, e declare os fragmentos lá, então você não precisará fazer nada de especial na atividade.
layout/my-layout
layout-land/my-layout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow