Domanda

Voglio modificare il layout del frammento quando la configurazione cambia utilizzando il frammento .. Ma il frammento si sovrappone al layout di attività quindi aiutami per questo come risolvere questo ... E questo file di 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();
}
.

Questo è il file XML per attività ..

<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" />
.

Questo è lo scatto ........

Inserisci Descrizione dell'immagine qui

È stato utile?

Soluzione

Stai dichiarando i frammenti in XML e programmaticamente nell'attività.Devi semplicemente scegliere un'opzione.

Ad esempio, crea due diversi file di layout uno per ritratto e uno per il paesaggio e dichiarare i frammenti lì quindi non è necessario fare nulla di speciale nell'attività.

layout/my-layout
layout-land/my-layout

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top