问题
I read this
" It used to be that Android would use a single pass to process RelativeLayout-defined rules. That meant you could not reference a widget (e.g., via android:layout_above) until it had been declared in the XML. This made defining some layouts a bit complicated. Starting in Android 1.6, Android uses two passes to process the rules, so you can now safely have forward references to as-yet-undefined widgets. "
I do not know what is the problem maybe is eclipse problem, but even I use 2.3 I still have problems when I reference some view that is not declared jet so for me it seems like android doesn't uses two passes to process the rules for relative layout.
note: I always use @+id/widget_name when I declare the widget and @id/widget_name when I reference that widget from other widget. I have noticed that I can use @+id/widget_name even when I just want to reference that widget. I guess that is wrong but why sometimes is works without any complaints ? In my opinion one widget should be allowed to be declared only ones...
My questions is is really android uses two passes ? and I need some guidelines (best practices) for working with relative layouts
I am little confused about how this relative layout parings are made, so any explanations are welcomed
Thanks
回答1:
@+id/name
creates a new id, if it doesn't already exist. @id/name
references an existing id, and will never create one.
I'm not sure if you can use @id/name
before @+id/name
in the same file. If not, I can think of two workarounds:
Always use
@+id/name
.Define all id's in the
ids.xml
file, and always use@id/name
.
回答2:
This is general information on how Android draw views. I think that Android passes twice through all the view, but it doesn't pass through each single view once. So if you have a reference from one xml to another it will always work fine, but if you have references inside a single xml you must be carefull to order the elements in the xml correctly. For example, I have view1 and view2 in my RelativeLayout. If I want to refer to view2 from view1 I must declare view2 before view1.
来源:https://stackoverflow.com/questions/7385453/order-of-evaluation-with-relative-layouts-best-practices-and-parsing-of-relativ