Java语法解释 – getMenuInflater()
刚刚下载了android studio,我正在使用大书呆子牧场的android编程指南来学习绳索。
当你启动android studio时,这段代码已经在主活动文件中了:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. **getMenuInflater().inflate(R.menu.menu_quiz, menu);** return true; }
我不明白getMenuInflater
行。 在我对java的简短经验中,只有一个对象出现在一个方法之前,当使用一个句点来分隔两个时,例如在dog.bark()
。 这里看起来像线意味着调用在getMenuInflater方法中定义的inflate方法。 但是,我检查了getMenuInflater()
的源代码,并且其主体中没有getMenuInflater()
方法。
有人可以为我揭开这一行的语法吗?
行getMenuInflater().inflate(R.menu.menu_quiz, menu);
这是一个简短的forms:
MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_quiz, menu)
你正在扩展一个活动,android studio为你配置了布局的菜单,下面是活动类中方法的实际实现:
public MenuInflater getMenuInflater() { // Make sure that action views can get an appropriate theme. if (mMenuInflater == null) { initWindowDecorActionBar(); if (mActionBar != null) { mMenuInflater = new MenuInflater(mActionBar.getThemedContext(), this); } else { mMenuInflater = new MenuInflater(this); } } return mMenuInflater; }
我不太确定你对此感到困惑,但我相信你认为你必须总是在一个对象的名字后面调用一个方法。 这基本上是正确的(除了静态方法),所以你可以把这个调用想象成
this.getMenuInflater()
这是Java中的一个关键字,指的是调用该方法的当前对象
这将类似菜单将在名为menu.xml的Res文件夹中自动创建,
您可以根据需要添加菜单列表
菜单看起来像