issue with “@id” vs “@+id” in positioning in Relative Layout

前端 未结 3 603
忘掉有多难
忘掉有多难 2020-12-01 23:56

Good day, i have a slight issue with relative layouts and i don\'t know why. Normally when you trying to position relative to other views, you use \"@id\" but it doesn\'t se

3条回答
  •  [愿得一人]
    2020-12-02 00:17

    @+id is used when declaring the ID for the first time, and you should not use it in the positioning of elements. It should be used only for the android:id attribute.

    @id on the other hand is used to reference an existing ID.

    In a RelativeLayout, you cannot reference views below the referencing view in the current code. So if you have the following layout:

    LayoutRoot
        View1
        View2
        View3
    

    View1 cannot, or rather should not, use View2 and View3 to define its position. View2 should use only View1 to define its position, and View3 can use both View1 and View2.

    Essentially, you can use @id whenever the view you're referencing through it has been declared above the referencing view in the layout file.

    So simply switching the code blocks' position of your last two widgets should allow you to use @id:

    
    
        
        
    
        
        
    
        
        
    
        
        
    
    
    

提交回复
热议问题