Link to another layout.xml file without duplication

I need to provide the same layout.xml file for Activity for several different classifiers. I know that there is a way to simply reference one existing layout.xml instead of actually copying it and having a duplicate.

But how? Can't find it in Android docs right now ...: - /

Is anyone faster than me?

EDIT: although I found this β€œsolution”, I'm still not there.

<?xml version="1.0" encoding="utf-8"?> <merge> <include layout="@layout/main_ltr"/> </merge> 

I need to specify a different qualifier layout file, not a different layout file in the same qualifier. The reason for this: I determined the new Android 3.2 classifier by proving screen width qualifiers. But on Android 3.0 / 3.1, this does not work, I need xlarge there, but I want it to be exactly the same file, not a copy!

+4
source share
3 answers

If I understood correctly, I have one layout file for xlarge and sw-600dp, and another for the rest. In any case, this was my situation when I came across this question.

This can be solved by creating layouts of the folders-xlarge and layout-s600dp and placing one layout file in them, but with the same contents. But for obvious reasons, I would like you to not have two identical files in two folders.

This is my solution to the problem. Do not create layout-xlarge and layout-sw600dp files. Let's say you create a cool job with the layout file / res / layout / cool _activity.xml. Create a tablet layout file in the same folder, but with a different name, i.e. / res / layout / cool _activity_for_tablet.xml, and then create the folder values ​​- xlarge and values-sw600dp with the layout.xml files in it with the following contents

 <?xml version="1.0" encoding="utf-8"?> <resources> <item name="cool_activity" type="layout">@layout/cool_activity_for_tablet</item> </resources> 

Yes, you will still have 2 of them with the same content, but this is silly content, not the layout itself, which can be hundreds of xml lines.

+7
source

<include> see Layout Tricks # 2

<merge> see Layout Tricks # 3

+4
source

Create the my_activity.xml wrapper my_activity.xml and your layouts for small and large devices.

 <merge> <include layout="@layout/my_activity_small.xml"/> </merge> 

Your resources should look like this:

 layout -> my_activity.xml -> my_activity_small.xml -> my_activity_xlarge.xml 

Now override my_activity.xml in layout-xlarge and layout-sw600dp :

 <merge> <include layout="@layout/my_activity_xlarge.xml"/> </merge> 

Your resources should look like this:

 layout -> my_activity.xml <-- includes my_activity_small.xml -> my_activity_small.xml -> my_activity_xlarge.xml layout-xlarge -> my_activity.xml <-- includes my_activity_xlarge.xml layout-sw600dp -> my_activity.xml <-- includes my_activity_xlarge.xml 

Use my_activity.xml in your code to load the layout.

PS: You cannot specify the layout in another catalog of classifiers, as you mentioned in one of the comments.

0
source

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


All Articles