What is setContentView(R.layout.main)?

2019-01-14 20:42发布

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!

6条回答
Viruses.
2楼-- · 2019-01-14 21:22
public void onCreate(Bundle savedinstanceState) {
            super.onCreate(savedinstanceState);

            Button testButon = new Button(this);

            setContentView(testButon);

            show();

}
查看更多
聊天终结者
3楼-- · 2019-01-14 21:27

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 this tutorial too.

查看更多
仙女界的扛把子
4楼-- · 2019-01-14 21:35

in Android the visual design is created in xml . And each Activity is associated to a design

setContentView(R.layout.main)

R means Resource

layout means design

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

Whenever you want to change your current Look of an Activity or when you move from one Activity to another . The other Activity must have a design to show . So we call this method in onCreate and this is the second statement to set the design

查看更多
不美不萌又怎样
5楼-- · 2019-01-14 21:40

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).
查看更多
该账号已被封号
6楼-- · 2019-01-14 21:41

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 Launcher activity 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.

查看更多
闹够了就滚
7楼-- · 2019-01-14 21:46

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

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

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

want to know more click on the link

http://androidride.com/what-is-setcontentview-in-android/

查看更多
登录 后发表回答