如何在导航抽屉内使用字符串数组
问题描述:
我想在MainActivity中使用字符串数组并在导航抽屉中显示我的项目。例如String[] name = {"a", "b", "d" ,"e"}
。我想在我的导航抽屉项目中看到这个数组列表。如何在导航抽屉内使用字符串数组
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
// suppose there is a string array
String[] name = {"a", "b", "d" ,"e"}
// now I want to show this array in my Navigation drawer item
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.setDrawerListener(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.main, 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_camara) {
// 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
我知道这种方式去菜单文件夹并在XML文件中添加项目菜单。但是我想从我的MainActivity java类中添加项目,而不是从XML文件中解析MaintActvity类中的Array String。 –
+0
然后实现NavigationView.OnNavigationItemSelectedListener不起作用,你必须编写另一种编码方式。但目前这是从xml使用抽屉的正确方式 – Faravy
是的,谢谢;-)你的问题是什么? (顺便说一下,请阅读:http://stackoverflow.com/help/mcve) – kebs
我已经提到它,我想显示项目从我的字符串数组中使用的导航抽屉项目java类 –