我已经搜索过我必须做这样的事情:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View view = navigationView.getHeaderView(0);
Menu menu = navigationView.getMenu();
menu.add("element");
但是如何处理他们的点击?我不能硬编码,因为它们甚至还不存在。
答案 0 :(得分:0)
MenuItem item = menu.add("element");
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
//your logic
return false;
}
});
答案 1 :(得分:0)
您可以提供其他视图作为NavigationView的子项
<android.support.design.widget.NavigationView>
<ScrollView>
<LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.design.widget.NavigationView>
答案 2 :(得分:0)
您必须为android.support.design.widget.NavigationView.OnNavigationItemSelectedListener
实现android.support.v7.app.AppCompatActivity
界面,并点击以编程方式添加的菜单项:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
...
@Override
public boolean onNavigationItemSelected(MenuItem item) {
String itemName = item.getTitle().toString();
if ("element".equals(itemName){
//Do what you want...
}
}
...
}