Reference the id of a view inside an “include” layout in xml

前端 未结 2 906
一向
一向 2021-01-13 02:06

I have a button, A, inside special_button.xml, that I reuse on all my activities. Each activity has a root RelativeLayout.

The

2条回答
  •  灰色年华
    2021-01-13 02:30

    When you use the include tag, you should also specify a new id within that XML, which you can then reference as the id within the RelativeLayout. See the documentation and this sample code:

    You can also override all the layout parameters (any android:layout_* attributes) of the included layout's root view by specifying them in the tag. For example:

    
    

    EDIT: As @eskimoapps.com points out, there appears to be a known issue doing this with RelativeLayouts, as documented here, but when I run the following code, it is working as OP requests:

    
    
    
        
    
        

    Here is the picture from HierarchyViewer showing the correct layout: id/A below id/B

    enter image description here

    My only thought is that Eclipse doesn't like it since it's statically trying to find the Button within the same layout file. Since and work dynamically with the LayoutManager, that's probably while this still works as expected.

    Please try and see if this works for you as well.

提交回复
热议问题