2017-12-19 13 views
0

メニュークラスを作成して、他のアクティビティをその1つから拡張しようとしました ボタンは表示されますが、スライドは他のアクティビティでは動作しませんが、コードナビゲーションメニューは子アクティビティでは機能しません

に行くといけない

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/drawer" 
     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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="ar.com.puebloyreforma.pyr.NavigationMenu"> 



     <android.support.design.widget.NavigationView 
      app:headerLayout="@layout/menuheader" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/caldroid_black" 
      app:itemTextColor="@color/white" 
      app:itemIconTint="@color/white" 
      app:menu="@menu/drawermenu" 
      android:layout_gravity="start" 
      >  


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

が、これは私のmenudrawer

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/noticias" android:title="Noticias" android:icon="@drawable/iconews"></item> 
    <item android:id="@+id/calendario" android:title="Calendario" android:icon="@drawable/icocalen"></item> 
    <item android:id="@+id/sysacad" android:title="Sysacad" android:icon="@drawable/icosysa"></item> 
    <item android:id="@+id/contacto" android:title="Contacto" android:icon="@drawable/icocon"></item> 
    <item android:id="@+id/reglamento" android:title="Reglamento" android:icon="@drawable/icoreg"></item> 

</menu> 

とhであるactivity_navigatoin_menu ...これは私のxmlですERE私の活動

package ar.com.puebloyreforma.pyr; 

import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.MenuItem; 

public class NavigationMenu extends AppCompatActivity { 
    private DrawerLayout mDL ; 
    private ActionBarDrawerToggle mT ; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_navigation_menu); 
     mDL = (DrawerLayout) findViewById(R.id.drawer) ; 
     mT = new ActionBarDrawerToggle(this , mDL ,R.string.open , R.string.close) ; 
     mDL.addDrawerListener(mT); 
     mT.syncState(); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (mT.onOptionsItemSelected(item)){ 
      return true ; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

、これはそのONWレイアウトを持つ1つのアクティビティ

package ar.com.puebloyreforma.pyr; 

import android.app.ActionBar; 
import android.content.Context; 
import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 

public class PdfsActivity extends NavigationMenu { 
    public String getTarget() { 
     return target; 
    } 

    public void setTarget(String target) { 
     this.target = target; 
    } 

    public String target ; 

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



     Button tt , tm , tn, reg ; 
     tt = (Button)this.findViewById(R.id.tt); 
     tm = (Button)this.findViewById(R.id.tm); 
     tn = (Button)this.findViewById(R.id.tn); 
     reg = (Button)this.findViewById(R.id.reg); 

     final String pdfs[] = {"tm" , 
       "tt" , 
       "tn", 
       "Ord1549" 
     } ; 

     tm.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       setTarget(pdfs[0]); 
       Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class); 
       hola.putExtra("TARGET", getTarget()); 
       startActivity(hola); 
      } 
     }); 



     tt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       setTarget(pdfs[1]); 
       Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class); 
       hola.putExtra("TARGET", getTarget()); 
       startActivity(hola); 
      } 
     }); 

     tn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       setTarget(pdfs[2]); 
       Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class); 
       hola.putExtra("TARGET", getTarget()); 
       startActivity(hola); 
      } 
     }); 
     reg.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       setTarget(pdfs[3]); 
       Intent hola = new Intent (PdfsActivity.this,ReglamentoActivity.class); 
       hola.putExtra("TARGET", getTarget()); 
       startActivity(hola); 
      } 
     }); 





    } 

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.constraint.ConstraintLayout 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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     tools:context="ar.com.puebloyreforma.pyr.PdfsActivity"> 



      <Button 
      android:id="@+id/tm" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="MAñANA" 


      android:layout_marginRight="60dp" 
      app:layout_constraintTop_toTopOf="parent" 
      android:layout_marginTop="8dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      android:layout_marginBottom="8dp" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintVertical_bias="0.758" 
      android:layout_marginLeft="8dp" 
      app:layout_constraintLeft_toLeftOf="parent" /> 

     <Button 
      android:id="@+id/tt" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TARDE" 


      android:layout_marginBottom="8dp" 
      app:layout_constraintTop_toTopOf="parent" 
      android:layout_marginTop="8dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintVertical_bias="0.335" 
      android:layout_marginRight="123dp" 
      app:layout_constraintRight_toRightOf="parent" /> 

     <Button 
      android:id="@+id/tn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="NOCHE" 
      app:layout_constraintTop_toTopOf="parent" 
      android:layout_marginTop="8dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      android:layout_marginBottom="8dp" 
      android:layout_marginLeft="68dp" 
      app:layout_constraintVertical_bias="0.572" 
      app:layout_constraintLeft_toLeftOf="parent" /> 

     <Button 
      android:id="@+id/reg" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="REGLA" 
      app:layout_constraintLeft_toRightOf="@+id/tn" 
      android:layout_marginLeft="0dp" 
      app:layout_constraintTop_toTopOf="parent" 
      android:layout_marginTop="8dp" 
      app:layout_constraintBottom_toBottomOf="parent" 
      android:layout_marginBottom="8dp" 
      android:layout_marginRight="8dp" 
      app:layout_constraintVertical_bias="0.572" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintHorizontal_bias="0.713" /> 
    </android.support.constraint.ConstraintLayout> 
+1

'onCreateOptionsMenu'方法はありますか? –

+1

私は1つ持っていない、私は父親の活動をlauchときにメニューが動作しますが、子供にはない – lukini

+0

あなたはコンテンツの表示自体を変更しています。まず、なぜあなたはアクティビティのために子供の親アーキテクチャを持つ必要がありますか? –

答えて

0

は私が私が子供の活動にNavigationDrawerを得るために管理していると私のコードを投稿してみましょうです。

MyAppCompatActivity(ParentActivity class

public abstract class MyAppCompatActivity extends BaseAppCompatActivity { 

    @Override 
    public void setContentView(@LayoutRes int layoutResID) { 
    CoordinatorLayout activityParentBase = (CoordinatorLayout) getLayoutInflater().inflate(R.layout.activity_navigation_drawer, null); 
    RelativeLayout content = (RelativeLayout) activityParentBase.findViewById(R.id.content); 
    setContentView(activityParentBase); 
    getLayoutInflater().inflate(layoutResID, content, true); 
    super.setContentView(activityParentBase); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    toolbar.setTitle(""); 
    setSupportActionBar(toolbar); 
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      MyAppCompatActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
      int id = item.getItemId(); 

      if (id == R.id.nav_item1) { 

      } else if (id == R.id.nav_item2) { 

      } 
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 
      return true; 
     } 
    }); 
    } 
} 

activity_navigation_drawer.xml(ParentActivity 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MyAppCompatActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     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.design.widget.AppBarLayout> 

     <android.support.v4.widget.DrawerLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/drawer_layout" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
      tools:openDrawer="start"> 

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

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

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

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

ChildNavigationActivity(ChildActivity class

public class ChildNavigationActivity extends MyAppCompatActivity { 

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //we have included "toolbar" in the parentactivity xml. So not need to do it in childactivity xml. 
    /**NOTE: Do Not use "setSupportActionBar(toolbar)" since you have already done it in your parent activity*/ 
    toolbar.setTitle("ChildNavigationActivity"); 

    } 

} 

activity_child_navigation(ChildActivity XML

<FrameLayout 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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".ChildNavigationActivity"> 

    <!--Put all your ChildActivity Widgets here--> 

</FrameLayout> 
+0

これを試して、私の結果に返信してください、これを行うには時間を取るためにもう一度 – lukini

+0

I LOVE U MAN!それは完全にスタイルのxmlといくつかの問題を持ってwokingしかし少しresech私は変更changinテーマに... light.NoActionBar – lukini