Is it possibly to have Views with the SAME id in a Tab Layout but in different Tab Contents?
I am using a Tab layout to display 3 different Content Views however there are several Views (Buttons to be precise) which are common to across all 3 Content Views. Currently I have uniquely identified all the common views e.g.
android:id=@+id/tab1_button1
even though they carry out the same function despite which Tab Content i开发者_JS百科s currently displayed.
Is there a way I can maintain the same View id across all the Tab Contents? Is it a matter of just identifying the Views with the same id?
Because I'm Using Tabs I understand that TabHost must be the root node of a Layout therefore I cannot have TabHost within another Layout. Do I understand this correctly?
You can use the same id across the tabs android:id=@+id/tab_button1
for button1
on all the tabs. However it isn't a good design, you'd better extract the similar parts of your layout as include to avoid code duplication. More info here.
As stated Here
The XML vocabulary for declaring UI elements closely follows the structure and naming of the classes and methods, where element names correspond to class names and attribute names correspond to methods.
Any View object may have an integer ID associated with it, to uniquely identify the View within the tree.
An ID need not be unique throughout the entire tree, but it should be unique within the part of the tree you are searching (which may often be the entire tree, so it's best to be completely unique when possible).
So you can use same id for any number of views. But best practice should be to use same id for similar views. You just have to remember that you should call findViewById()
only on a view whose child view you seek to.
If you call findViewById()
on a view which has more than one child views with same ids, then the behavior is undocumented and its not wise to depend on its current implementation.
精彩评论