Android 什么是 setContentView(R.layout.main)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24706348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:37:39  来源:igfitidea点击:

What is setContentView(R.layout.main)?

androidlayout

提问by user3814312

I understand that it has to do with the App layout, but when do I have to use it? I tried to look for a link that explained this method, but I couldn't find it. Thank you in advance!

我知道它与 App 布局有关,但我什么时候必须使用它?我试图寻找解释此方法的链接,但找不到。先感谢您!

回答by Zare Ahmer

In Android the visual design is stored in XML files and each Activityis associated to a design.

在 Android 中,视觉设计存储在 XML 文件中,每个Activity都与一个设计相关联。

setContentView(R.layout.main)

Rmeans Resource

R表示资源

layoutmeans design

layout意味着设计

mainis the xml you have created under res->layout->main.xml

main是您在下面创建的 xml res->layout->main.xml

Whenever you want to change the current look of an Activity or when you move from one Activity to another, the new Activity must have a design to show. We call setContentViewin onCreate with the desired design as argument.

每当您想要更改 Activity 的当前外观或从一个 Activity 移动到另一个 Activity 时,新 Activity 都必须具有要显示的设计。我们setContentView以所需的设计作为参数调用onCreate 。

回答by CodeWarrior

As per the documentation :

根据文档:

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

从布局资源设置活动内容。资源将被膨胀,将所有顶级视图添加到活动中。

Your Launcheractivity in the manifest first gets called and it set the layout view as specified in respective java files setContentView(R.layout.main);. Now this activity uses setContentView(R.layout.main)to set xml layout to that activity which will actually render as the UI of your activity.

Launcher在清单中的活动首先被调用,并按照各自的 java 文件中的指定设置布局视图setContentView(R.layout.main);。现在,此活动用于setContentView(R.layout.main)将 xml 布局设置为该活动,该活动将实际呈现为您的活动的 UI。

回答by Thomas Daniel

Why setContentView() in Android Had Been So Popular Till Now?

为什么 Android 中的 setContentView() 一直流行到现在?

setContentView(int layoutid) - method of activity class. It shows layout on screen.

setContentView(int layoutid) - 活动类的方法。它在屏幕上显示布局。

R.layout.main - is an integer number implemented in nested layout class of R.java class file.

R.layout.main - 是在 R.java 类文件的嵌套布局类中实现的整数。

At the run time device will pick up their layout based on the id given in setcontentview() method.

在运行时,设备将根据 setcontentview() 方法中给出的 id 选择它们的布局。

回答by Babavali Sheik

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

从布局资源设置活动内容。资源将被膨胀,将所有顶级视图添加到活动中。

  • Activity is basically a empty window
  • SetContentView is used to fill the window with the UI provided from layout file incase of setContentView(R.layout.somae_file).
  • Here layoutfile is inflated to view and added to the Activity context(Window).
  • Activity 基本上是一个空窗口
  • SetContentView 用于使用布局文件提供的 UI 填充窗口,以防 setContentView(R.layout.somae_file)。
  • 这里布局文件被膨胀以查看并添加到活动上下文(窗口)。

回答by Lazy

You can set content view (or design) of an activity. For example you can do it like this too :

您可以设置活动的内容视图(或设计)。例如,您也可以这样做:

public void onCreate(Bundle savedinstanceState) {
    super.onCreate(savedinstanceState);

    Button testButon = new Button(this);

    setContentView(testButon);   
}

Also watch thistutorial too.

也看这个教程。

回答by Rodolfo Samuel Gavilan Mu?oz

public void onCreate(Bundle savedinstanceState) {
            super.onCreate(savedinstanceState);

            Button testButon = new Button(this);

            setContentView(testButon);

            show();

}