2013-06-15 15 views
5

Ho un'applicazione che utilizza i frammenti. Su uno dei dispositivi di mio utente (HTC One), i frammenti si sovrappongono l'un l'altro e il suo schermo finisce per sembrare un pasticcio:Le schermate dei frammenti di Android si sovrappongono a

enter image description here

Ho cercato di riprodurlo sul mio hardware, anche se è non l'HTC One. Ho anche provato ad usare la versione 4.1.2 di Android, che è la versione che ha e funziona bene. A corto di corsa a comprare un HTC One, qualcuno ha qualche suggerimento?

Quando aggiungo nel nuovo frammento, faccio questo layout

Fragment f = new MyFragment(); 
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); 
ft.replace(R.id.mainContent, f); 
ft.addToBackStack(null); 
ft.commit(); 

mio XML (tagliato fino a parti rilevanti):

<RelativeLayout> 
    <LinearLayout> 
     <!-- My home screen content is here --> 
    </LinearLayout> 

    <!-- This is where the fragment gets placed --> 
    <LinearLayout android:id="@+id/mainContent" 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    </LinearLayout> 
</RelativeLayout> 

UPDATE

Il frammento che viene aggiunto ha questo layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@drawable/app_bg"  
    tools:context=".DeviceListActivity" > 

    <!-- snipped! (for brevity) --> 
</RelativeLayout> 

Ho giocato in giro - ho notato che se rimuovo l'androide: background posso riprodurre il problema che mi porta a credere che l'HTC One stia causando l'ignizione della proprietà di background del frammento per qualche motivo.

+0

L'HTC One utilizza le risorse xxhdpi, li fornite? Sembra che possa essere correlato. – Dalmas

+0

@Dalmas, che non causerebbe problemi di layout, causerebbe problemi di risorse, ovvero la risorsa errata caricata. –

risposta

2

si scopre il problema era un problema di risorse, ma non è chiaro il motivo per cui ...

Lo sfondo che avevo era solo nella cartella ldpi; normalmente le densità più alte useranno qualsiasi grafico disponibile, tuttavia in questo caso il dispositivo ignorava quel file. Un insetto? Non so - in ogni caso, la correzione è stata quella di creare una risorsa per la cartella xxdpi che corrisponde allo stesso nome.

0

Si può provare a cambiare il contenitore dei frammenti in un FrameLayout e vedere se ciò accade ancora. Dato che hai uno sfondo sul tuo frammento, dovrebbe coprire quello precedente. Non sono sicuro del motivo per cui ciò accade solo su quel dispositivo.

<!-- This is where the fragment gets placed --> 
<FrameLayout android:id="@+id/mainContent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
4

Ho riscontrato una situazione simile e il seguente ha funzionato per me.

Verificare se il parametro SavedInstanceState se non-null, in tal caso, non eseguire la transazione del frammento. Risolverà questo problema.

if (homeFragment == null) { 
    if (savedStateInstance != null) { 
    homeFragment = (HomeFragment) fragmentManager.findFragmentByTag(HOME_FRAGMENT); 
    return; 
} 
// tie new fragment 
homeFragment = new HomeFragment(); 
homeFragment.setArguments(bundle); 
FragmentTransaction transaction = fragmentManager.beginTransaction(); 
transaction.add(homeFragment, HOME_FRAGMENT); 
transaction.commit(); 
} 

Spero che questo aiuti.

0

ho fatto ...

mio layout mainactivity

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="@color/colorPrimary" 
    android:padding="10dp"> 

    <RelativeLayout 
     android:id="@+id/registerContainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/usernameEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="15dp" 
      android:layout_marginTop="15dp" 
      android:hint="USERNAME" 
      android:textColorHint="#FFFFFF" 
      android:textColor="#FFFFFF" 
      android:background="@drawable/edittext_style" 
      android:textAlignment="center"/> 

     <EditText 
      android:id="@+id/passEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="15dp" 
      android:layout_marginTop="15dp" 
      android:inputType="textPassword" 
      android:hint="PASSWORD" 
      android:textColorHint="#FFFFFF" 
      android:textColor="#FFFFFF" 
      android:textAlignment="center" 
      android:background="@drawable/edittext_style" 
      android:layout_below="@id/usernameEditText"/> 


     <EditText 
      android:id="@+id/phoneEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="15dp" 
      android:layout_marginTop="15dp" 
      android:inputType="phone" 
      android:hint="PHONE NUMBER" 
      android:textColorHint="#FFFFFF" 
      android:textColor="#FFFFFF" 
      android:background="@drawable/edittext_style" 
      android:textAlignment="center" 
      android:layout_below="@id/passEditText"/> 

     <EditText 
      android:id="@+id/emailEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="15dp" 
      android:layout_marginTop="15dp" 
      android:inputType="textEmailAddress" 
      android:hint="EMAIL" 
      android:textColorHint="#FFFFFF" 
      android:textColor="#FFFFFF" 
      android:background="@drawable/edittext_style" 
      android:textAlignment="center" 
      android:layout_below="@id/phoneEditText"/> 

     <Button 
      android:id="@+id/registerEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/emailEditText" 
      android:text="REGISTER" 
      android:layout_marginTop="20dp" 
      android:background="#0b999b" 
      android:textColor="#000000" 
      android:textSize="18dp"/> 


    </RelativeLayout> 

    <FrameLayout 
     android:id="@+id/otpfragmentcontainer" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"></FrameLayout> 


</RelativeLayout> 

frammento a sovrapporsi layout è otp_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="@color/colorPrimary"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/textviewOTP" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Enter Your OTP" 
      android:textColor="#0b999b" 
      android:textStyle="bold" 
      android:textSize="24dp" 
      android:textAlignment="center" 
      android:layout_marginTop="200dp" 
      android:layout_marginBottom="5dp"/> 

     <EditText 
      android:id="@+id/otpEditText" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:padding="15dp" 
      android:layout_marginTop="15dp" 
      android:inputType="phone" 
      android:hint="EX: 456789" 
      android:textColorHint="#FFFFFF" 
      android:textColor="#FFFFFF" 
      android:background="@drawable/edittext_style" 
      android:textAlignment="center" 
      android:layout_below="@id/textviewOTP"/> 

     <Button 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:text="ENTER" 
      android:background="#0b999b" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/otpEditText"/> 


    </RelativeLayout> 

</LinearLayout> 

quello che ho fatto è

 registerContainer.setVisibility(View.GONE); 

     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     OTPFragment otpFragment = new OTPFragment(); 
      fragmentTransaction.add(R.id.otpfragmentcontainer, otpFragment); 
      fragmentTransaction.commit(); 

which disable registerContainer and display my fragment layout 
Problemi correlati