what is ids.xml used for?

后端 未结 5 591
予麋鹿
予麋鹿 2020-12-13 23:40

Just a quick question, what is the ids.xml used for when developing an Android app? I saw an example on the android resources webpage which contained:



        
5条回答
  •  失恋的感觉
    2020-12-14 00:00

    Another application for id.xml is in respect to layouts and library projects. Let's say you specify a generic list of options in a library (dialog) layout

    and handle these views in a generic (dialog) fragment

    optionOneCheck = (CheckedTextView)rootView.findViewById(R.id.checked_option_one);
    optionTwoCheck = (CheckedTextView)rootView.findViewById(R.id.checked_option_two);
    

    If you remove any of the view declarations from a copy of the layout in a main project, you will get a "no such field" error exception at runtime.

    The compiler doesn't complain, but at runtime the id isn't actually there/known.

    Declaring the ids in id.xml and using

    avoids the runtime error

提交回复
热议问题