如何在我的 android 工作室项目中找到根视图?
how do i find the root view in my android studio project?
我正在使用 android 工作室提供的导航菜单模板,我注意到主 activity 将内容视图设置为根视图,但我不确定哪个 XML 文件是什么?我假设它可能是 XML 视图,当我 运行 应用程序(将是 fragment_home.xml)时首先加载,但我不确定是否放置了任何特定代码在根视图的 xml 或 java 文件中?
这是主要代码 activity.java:
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
1. ON CREATE, SET MAIN SCREEN TO
here instead of setting the main screen with findViewById, we are using BINDING!
this is part of android jetpack!
*/
binding = ActivityMainBinding.inflate(getLayoutInflater()); // inflate method creates an instance of the binding class for this activity to use
setContentView(binding.getRoot()); // getRoot: a reference to the root view and passing it to setcontentview to make it the active view on the screen
// when running the app, fragment_home shows up first. hence, this is the root view
setSupportActionBar(binding.appBarMain.toolbar);
binding.appBarMain.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 = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
和项目结构的图片:
activity_main.xml
ActivityMainBinding 由 viewbinding/databinding 生成,如果您 cmd/ctrl 单击它,它将带您到 activity_main.xml
我正在使用 android 工作室提供的导航菜单模板,我注意到主 activity 将内容视图设置为根视图,但我不确定哪个 XML 文件是什么?我假设它可能是 XML 视图,当我 运行 应用程序(将是 fragment_home.xml)时首先加载,但我不确定是否放置了任何特定代码在根视图的 xml 或 java 文件中?
这是主要代码 activity.java:
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
1. ON CREATE, SET MAIN SCREEN TO
here instead of setting the main screen with findViewById, we are using BINDING!
this is part of android jetpack!
*/
binding = ActivityMainBinding.inflate(getLayoutInflater()); // inflate method creates an instance of the binding class for this activity to use
setContentView(binding.getRoot()); // getRoot: a reference to the root view and passing it to setcontentview to make it the active view on the screen
// when running the app, fragment_home shows up first. hence, this is the root view
setSupportActionBar(binding.appBarMain.toolbar);
binding.appBarMain.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 = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@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 onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
和项目结构的图片:
activity_main.xml
ActivityMainBinding 由 viewbinding/databinding 生成,如果您 cmd/ctrl 单击它,它将带您到 activity_main.xml