Next view id as a custom attribute in android

不羁岁月 提交于 2019-12-23 10:06:43

问题


For my custom view I have also defined a custom attribute for keep id of the view. Its format is "reference".

in layout xml it is defined like below, very similar with android:layout_below attr

<mycustomview id="@+id/cv_1" xyz:nextviewId="@id/cv_2"... />
<mycustomview id="@+id/cv_2" xyz:nextviewId="@id/cv_3"... />
...
<LinearLayout ...>
    <mycustomview id="@+id/cv_3" xyz:nextviewId="@id/cv_4"... />
</LinearLayout>
...

it gives me error I think it is because it is not declared yet.

Any suggestion for accessing the next object similar to this approach!!!

I am thinking to use tag attr for the next object find the next one with findByTag function. Is this a good way to do it.

Thanks a lot.


回答1:


Change your xml to:

...
<mycustomview id="@+id/cv_1" xyz:nextviewId="@+id/cv_2"... />
<mycustomview id="@+id/cv_2" xyz:nextviewId="@+id/cv_3"... />
...

(note the @+id in nextviewId)

This will work on Android 1.6+ (Api Level 4+). Exactly the same approach is used in RelativeLayouts.



来源:https://stackoverflow.com/questions/6426930/next-view-id-as-a-custom-attribute-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!