When should the dimens.xml file be used in Android?

后端 未结 3 1097
旧巷少年郎
旧巷少年郎 2020-12-13 20:14

For instance, in a specific layout I have the following XML:



        
3条回答
  •  攒了一身酷
    2020-12-13 20:49

    I drop dimension values into a dimens.xml resource typically for three reasons:

    1. Reuse: I need multiple widgets or layouts to use the same value and I only want to change it once when updating or tweaking across the application.

    2. Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi or small -> large.

    3. Reading in from code: When I'm instantiating a view in the code and want to apply some static dimensions, putting them in dimens.xml as dp (or dip) allowing me to get a scaled value in Java code with Resources.getDimensionPixelSize().

提交回复
热议问题