的setContentView(R.layout.activity_main)VS getMenuInflater()。膨胀(R.menu.activity_main,菜单) [英] setContentView(R.layout.activity_main) vs getMenuInflater().inflate(R.menu.activity_main, menu)

查看:487
本文介绍了的setContentView(R.layout.activity_main)VS getMenuInflater()。膨胀(R.menu.activity_main,菜单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我要告诉我的活动它的布局应该是两倍?

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?

推荐答案

他们是两个不同的东西。名字告诉你。 R.layout.activity_main 是布局, R.menu.activity_main 菜单

They are two separate things. The names tell you. R.layout.activity_main is your layout, R.menu.activity_main is for your menu.

的setContentView()设置为活动的布局。它包括按钮,TextViews等。

setContentView() sets the layout for the Activity. It includes Buttons, TextViews, etc.

onCreateOptionsMenu()让你看到,当你preSS菜单键在菜单或填充动作条上的Andr​​oid 3.0 +

onCreateOptionsMenu() makes the menu that you see when you press the menu key or it populates the ActionBar on Android 3.0+.

他们做两completetly独立的东西。 的setContentView()往往需要(除非你有一个空的活动), onCreateOptionsMenu() 是可选的,取决于是否需要显示更多选项上。

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(R.layout.activity_main)VS getMenuInflater()。膨胀(R.menu.activity_main,菜单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆