简体   繁体   English

setContentView(R.layout.activity_main)vs getMenuInflater()。inflate(R.menu.activity_main,menu)

[英]setContentView(R.layout.activity_main) vs getMenuInflater().inflate(R.menu.activity_main, menu)

Why do I have to tell my activity what its layout should be twice? 为什么我必须告诉我的活动它的布局应该是两次?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // <--
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu); // <--
    return true;
}

What's the difference between these two methods?. 这两种方法有什么区别? when should I use one, and when the other? 什么时候应该使用一个,何时另一个?

They are two separate things. 它们是两个不同的东西。 The names tell you. 这些名字告诉你。 R.layout.activity_main is your layout, R.menu.activity_main is for your menu . R.layout.activity_main是你的布局, R.menu.activity_main是你的菜单

setContentView() sets the layout for the Activity. setContentView()设置Activity的布局。 It includes Buttons, TextViews, etc. 它包括按钮,TextViews等。

onCreateOptionsMenu() makes the menu that you see when you press the menu key or it populates the ActionBar on Android 3.0+. onCreateOptionsMenu()当您按下菜单键时看到的菜单,或者它在Android 3.0+上填充ActionBar。

They do two completetly separate things. 他们完成了两件完全不同的事情。 setContentView() is often needed (unless you have an empty Activity ), onCreateOptionsMenu() is optional, depending on if you need to show more options. 通常需要setContentView() (除非你有一个空的Activity ), onCreateOptionsMenu()是可选的,具体取决于你是否需要显示更多选项。

java file inside the gen folder there will be defined layout, ID and menu static class. gen文件夹里面的java文件会有定义的布局,ID和菜单静态类。 you will get the idea from there. 你会从那里得到这个想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio 中的应用程序崩溃。 getMenuInflater().inflate(R.menu.activity_main, menu); 不存在? - App crashes in Android Studio. getMenuInflater().inflate(R.menu.activity_main, menu); doesn't exist? 错误 setContentView(R.layout.activity_main) - error setContentView(R.layout.activity_main) setContentView(R.layout.activity_main) 上的异常; - Exception on setContentView(R.layout.activity_main); 如果“ Activate Class”中没有“ getMenuInflater()。inflate(R.menu.main,menu)”,那么为什么Android Studio中的默认项目运行良好? - If “getMenuInflater().inflate(R.menu.main, menu)” is not from Activity Class, so why default project in android studio works well? R.layout.activity_main,NoClassDefFound错误 - R.layout.activity_main, NoClassDefFound error 如何修复程序包R不存在setContentView(R.layout.activity_main)及其后果? - How to fix package R does not exist setContentView(R.layout.activity_main) and it consequences? 调用 setContentView(R.layout.activity_main); 时应用程序崩溃; 第二次 - App crashing when calling setContentView(R.layout.activity_main); for the second time 为什么 setContentView(R.layout.activity_main) 和 findViewById(R.id.example) 不使用点符号来引用 object? - Why do setContentView(R.layout.activity_main) and findViewById(R.id.example) not use dot notation to reference an object? 如何在单击按钮时将contentview设置为R.layout.activity_main - How to set contentview as R.layout.activity_main on button click android开发中R.layout.activity_main的含义(JAVA语言) - Meaning of R.layout.activity_main in android development (JAVA language)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM