What is the concept behind R.java?

后端 未结 4 935
孤独总比滥情好
孤独总比滥情好 2020-12-01 13:29

In android R.java is used to provide access to resources defined in XML files. To access the resource we need to invoke findViewById() method passing in the id

4条回答
  •  孤街浪徒
    2020-12-01 14:25

    Biggest advantage is in Localization and Providing alternate resources for different screen sizes.

    e.g you can have a String resource R.string.myname this could be a defined in english in /values-en/strings.xml and in spanish in /values-es/strings.xml

    System will take care or picking up the right file depending on the locale you just need to use @string/myname in your layout file or R.string.myname in your code.

    Similarly you could have two layout files for portrait and landscape defined in

    res/layout/mylayout.xml
    res/layout-land/mylayout.xml
    

    In your code you will just specify R.layout.mylayout to inflate the layout. Resource manager picks up the file in layout-land if the device is in landscape mode.

    Doing this manually would be a nightmare -- hence the need for R file

提交回复
热议问题