2017-05-29 1 views
0

Je n'ai pas pu résoudre ce problème.
enter image description hereSuperposition de fragment ActionBar et contenu manquant dans le fragment

Voici mon Main activity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, AttendanceFragment.OnFragmentInteractionListener { 

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

    ButterKnife.bind(this); 

    setSupportActionBar(toolbar); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, R.string.nav_drawer_open, R.string.nav_drawer_close); 

    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView.setNavigationItemSelectedListener(this); 

    showAttendanceFragment(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.nav_drawer, menu); 
    return true; 
} 
private void showAttendanceFragment() { 
    AttendanceFragment fragment = new AttendanceFragment(); 
    FragmentManager fragmentManager = this.getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment_container,fragment); 
    fragmentTransaction.commit(); 
} 


@Override 
public void onFragmentInteraction(Uri uri) { 

} 

}

Voici le main xlm:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start" 
    android:background="@color/primary"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:itemTextColor="@color/accent" 
     app:itemIconTint="@color/accent" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" 
     android:background="@color/primary"/> 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize"/> 

</android.support.v4.widget.DrawerLayout> 

Le bar:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/primary_darker" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    </android.support.design.widget.AppBarLayout> 

</android.support.design.widget.CoordinatorLayout> 

et fragment:

<FrameLayout 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" 
    tools:context="com.example.hoangdang.diemdanh.Fragments.AttendanceFragment"> 

    <TextView 
     android:id="@+id/id1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 
    <TextView 
     android:id="@+id/id2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment" /> 
</FrameLayout> 

J'ai essayé l'utilisation linear layout mais ne fonctionne pas

Répondre

1

Vous devez comprendre les DrawerLayout. Vous pouvez le vérifier ici: Google DrawerLayout Guide. Comme la note de Google Guide:

La vue principale du contenu (le FrameLayout ci-dessus) doit être le premier enfant du DrawerLayout parce que l'ordre XML implique z-commande et le tiroir doit être au-dessus du contenu.

La vue du contenu principal est définie pour correspondre à la largeur et à la hauteur de la vue parente, car elle représente l'intégralité de l'interface utilisateur lorsque le tiroir de navigation est masqué.

La vue de tiroir (ListView) doit spécifier sa gravité horizontale avec l'attribut android: layout_gravity. Pour prendre en charge les langues de droite à gauche (RTL), spécifiez la valeur avec "start" au lieu de "left" (de sorte que le tiroir apparaît sur la droite lorsque la mise en page est RTL).

La vue de tiroir spécifie sa largeur en unités dp et la hauteur correspond à la vue parente.La largeur du tiroir ne doit pas dépasser 320dp afin que l'utilisateur puisse toujours voir une partie du contenu principal.

Comme on peut le voir dans votre xml:

  1. votre point de vue du contenu principal est AppBar, ce n'est pas bon.
  2. Les deux AppBar et FrameLayout ont match_parent dans layoutParams, c'est la raison pour laquelle vous avez le problème.

Alors, comment le résoudre? vous devez combiner AppBar et FrameLayout sous 1 vue seulement (Relative ou LinearLayout) et le mettre comme le premier enfant de DrawerLayout

0

Ajouter un comportement de mise en page et essayer ...

<FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
+0

merci pour la réponse, mais ce n'est qu'une partie de la solution, vous pouvez vérifier l'explication et l'exemple de code pour @FAT et Kingfisher Phuoc réponses –

1

1. Retirez votre FrameLayout de activity_main.xml et placez-le à app_bar_main.xml sous AppBarLayout.

2. Retirez attribute android:layout_marginTop="?attr/actionBarSize" de FrameLayout 2. Ajouter un attribut app:layout_behavior="@string/appbar_scrolling_view_behavior"-FrameLayout.

Mettez à jour votre activity_main.xml comme ci-dessous:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start" 
    android:background="@color/primary"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:itemTextColor="@color/accent" 
     app:itemIconTint="@color/accent" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" 
     android:background="@color/primary"/> 

</android.support.v4.widget.DrawerLayout> 

Mise à jour app_bar_main.xml comme ci-dessous:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/primary_darker" 
      app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    </android.support.design.widget.AppBarLayout> 

    <!-- Content :: Fragments--> 
    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

Espérons que cela fonctionnera ~

+0

merci pour l'exemple de code, cela a fonctionné –

+0

Si ma réponse aide à résoudre votre problème, alors vous devez le marquer comme bonne réponse. – FAT

+0

j'ai déjà marqué autre réponse parce qu'il a expliqué en détail non seulement le code –