Android - how do you manage multiple layout files according to DRY principle?

To support different resolutions, we need to do very well the layout file options described in "Multiple Screen Support." Assuming you don’t plan to show different settings of your user interface, but just want to stretch accordingly, your options will mainly concern different weights. At least this applies to my application.

Now, how do you manage to modify the application using this structure? Since it repeats the layout many times, a single change in the layout in the application changes several files.

I thought of two options:

  • Dynamically changing values ​​in your code
    • Downside - your work related to the layout spills over into the code. I really don't like it.
  • Create a child layout to retrieve common layout elements
    • Downside - your layout hierarchy will be deeper and cluttered, so it would be harder to understand what is happening. However, this is better than option # 1 thanks to the Hierarchical Browser . I am not sure that this approach will always be achievable.

If you could share your tricks to get through this, it would be very grateful.

+4
source share
1 answer

I think I found a solution. I will accept this as an answer if others give thumbs up.

I found the configuration options described in Support for multiple screens works not only for res/drawable and res/layout , but also for res/values . So, on my layout/some_layout.xml , I say this:

 <ImageButton android:id="@+id/imagePlay" android:layout_width="@dimen/button_size" android:layout_height="@dimen/button_size" android:scaleType="fitCenter" android:src="@drawable/play" /> 

Then, in the values/layout.xml you define the default value of button_size :

 <resources> <dimen name="button_size">44dp</dimen> </resources> 

And in the values-xlarge/layout.xml you define the button_size mode:

 <resources> <dimen name="button_size">66dp</dimen> </resources> 

I have not tried other values resources, but I believe that it also works for Styles and Themes , so in case your layout setting is slightly larger than size or weight, you can define the style in the values ​​and use it.

+1
source

Source: https://habr.com/ru/post/1436537/


All Articles