Referencing another layout.xml file without duplicating it

后端 未结 3 861
余生分开走
余生分开走 2020-12-17 03:49

I need to provide the same layout.xml file for an Activity for several different qualifiers. I know that there\'s a way to just reference one existing layout.xml instead of

3条回答
  •  生来不讨喜
    2020-12-17 04:32

    Create a wrapper layout my_activity.xml and your layouts for small and xlarge devices.

    
        
    
    

    Your resources should look like:

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

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

    
        
    
    

    Your ressources should look like:

    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 for loading the layout.

    P.S.: You can not point to a layout in another qualifier's directory, as you mentioned in one of the comments.

提交回复
热议问题