将导航抽屉添加到Android Studio中的我的应用程序
问题描述:
我刚开始使用Android Studio,对我而言,一切都相对较新。我为我的应用程序添加了一个新的导航抽屉活动,但它似乎没有我的栏上的切换按钮。我会去启用它。我可以在导航抽屉的类中看到已经存在切换代码。我已经在互联网上搜索了很长时间,我发现的所有内容都是将导航抽屉首先作为其起始模板的教程,或者是从头开始主要活动的直接创建。将导航抽屉添加到Android Studio中的我的应用程序
我的主要活动:
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;
}
}
答
把
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar()setDisplayShowHomeEnabled(真);
setSupportActionBar(toolbar);
嗨了!感谢您的快速回复。我添加了 'getSupportActionBar()。setDisplayHomeAsUpEnabled(true);' 'setSupportActionBar(toolbar);'但我的酒吧里依然没有任何东西。然后,我添加了 getSupportActionBar()。setDisplayHomeAsUpEnabled(true); 在我的主要活动中的onCreate方法下,它出现了,但无法点击。我是否需要将我的导航器从我的主体中扩展出来? –
你还加了'getSupportActionBar()。setDisplayShowHomeEnabled(true);' –
是的,最后,我开始了一个新项目,选择抽屉模板,并将所有代码放在新项目的主要活动中。我认为我的问题是无法连接原始项目中的两个java文件。我的所有应用程序代码都放在我的主文件夹上,而另一个java文件上的所有导航代码都放在了。 –