2016-11-24 3 views
-2

Androidスタジオで仕事を始めたばかりです。私はアプリケーションに新しいナビゲーション・ドロワ・アクティビティを追加しましたが、バーにトグル・ボタンがないようです。私はそれを可能にするつもりです。ナビゲーション・ドロワーのクラスの中に、既にトグルするコードがあることがわかります。私は本当に長い時間インターネットを検索しましたが、私が見つけたのは、ナビゲーション・ドロワを最初のテンプレートとして作成するチュートリアルか、メイン・アクティビティから最初からまっすぐに作成するチュートリアルです。Androidスタジオのアプリにナビゲーションドロワーを追加する

私の主な活動:

public class MainActivity extends AppCompatActivity { 


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


    } 

    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.top_menu, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 

     if(id == R.id.info) { 
      Toast.makeText(this,"Information", Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(this, Information.class); 
      startActivity(intent); 
     } 
     return super.onOptionsItemSelected(item); 
    } 

ザ・ナビゲーション・ドロワー(タッチされていません):

public class navigation_drawer extends AppCompactActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_navigation_drawer); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       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(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.navigation_drawer, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

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

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

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

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

     } 

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

答えて

0

)がgetSupportActionBar(

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

を入れsetDisplayShowHomeEnabled(真)。

 setSupportActionBar(toolbar); 
+0

ねえ!早速のお返事ありがとうございます。私は 'getSupportActionBar()を追加しました。setDisplayHomeAsUpEnabled(true);' の後に 'setSupportActionBar(toolbar);'私のバーにはまだ何もありません。次に、 getSupportActionBar()を追加しました。setDisplayHomeAsUpEnabled(true); 私の主な活動でonCreateメソッドの下に表示されたが、クリックすることができませんでした。私は私のメインからナビゲーションドローを延長する必要がありますか? –

+0

'getSupportActionBar()。setDisplayShowHomeEnabled(true);'を追加しました。 –

+0

はい、結局、私は新しいプロジェクトを開始しました。引き出しテンプレートを選択し、すべてのコードを新しいプロジェクトのメインアクティビティに入れました。私の問題は、元のプロジェクトの2つのJavaファイルを接続することができないと思う。私は主にすべての私のアプリケーションコードを持っていた、そして別のJavaファイル上のすべてのナビゲーションコード。 –

関連する問題