J'essaie d'implémenter le menu du tiroir de navigation qui s'affiche avec la barre d'outils pour chaque activité. J'ai décidé de créer une classe de menuactivity commune, et toutes les autres activités en découlent. Cela fonctionne bien, sauf qu'il met l'activité sous la barre d'outils de nav. menu.Impossible d'obtenir le contenu de l'activité sous la barre d'outils du menu du tiroir de navigation
MenuActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
...
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
changeActivity();
}
};
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setItemIconTintList(null);
navigationView.getMenu().getItem(currentMenuItem).setChecked(true);
...
à la même classe que je gère les élément touches (sélection de menu):
Intent intent = new Intent(getApplicationContext(), TrainingActivity.class);
startActivity(intent);
Et TrainingActivity:
public class TrainingActivity extends MenuActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setStartingState(MenuItemNames.TRAINING);
super.onCreate(savedInstanceState);
FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_frame);
View contentView = getLayoutInflater().inflate(R.layout.activity_training, contentLayout, false);
drawer.addView(contentView, 0);
// setContentView(R.layout.activity_training);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
...
Voici activity_menu .xml:
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/app_bar_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>
</LinearLayout>
<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:menu="@menu/activity_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Le app_bar_menu.xml:
<android.support.design.widget.CoordinatorLayout 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/app_bar_menu_coord_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.magnifi.pennantrace.MenuActivity">
<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="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:id="@+id/toolbar_div_rank"
android:text="4th Place"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
...
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
donc la logique que je suis en train de mettre en œuvre est que chaque fois que l'élément sélectionné dans le menu, je suis le chargement d'activité approprié (gonflage) dans le FrameLayout (sous content_frame la barre d'outils dans activity_menu.xml). Donc, il semble que ce soit sous la barre d'outils, mais ce n'est pas le cas. Pourriez-vous m'aider et me dire où est mon erreur?
'drawer.addView (contentView, 0);' - Vous voulez ajouter '' contentView' à contentLayout', pas 'drawer' –
@ MikeM. si je fais le contentLayout.addView (contentView, 0); alors il ne montre rien d'autre que vide, sauf menu/barre d'outils. – maximus
Oui, juste remarqué, vous avez deux choses à l'intérieur du 'LinearLayout 'vertical qui contient' content_frame', et les deux ont des hauteurs 'match_parent', donc' content_frame' est poussé au fond. Vous voulez probablement déplacer 'content_frame' dans' app_bar_menu', et vous débarrasser de l'élément 'LinearLayout 'dans le' DrawerLayout'. –