`
kanwoerzi
  • 浏览: 1647275 次
文章分类
社区版块
存档分类
最新评论

关于findViewById

 
阅读更多
android.app.Activity中
publicViewfindViewById(intid)
Since:APILevel1

FindsaviewthatwasidentifiedbytheidattributefromtheXMLthatwasprocessedinonCreate(Bundle).
Returns

*Theviewiffoundornullotherwise.
这里的findViewById是在当前Active的View根及其儿子中通过Id寻找View
Active的View根一般是在其在onCreate函数中可以通过setContentView(R.layout.main)这种形式来设置。

android.view.View中

publicfinalViewfindViewById(intid)
Since:APILevel1

Lookforachildviewwiththegivenid.Ifthisviewhasthegivenid,returnthisview.
Parameters
idTheidtosearchfor.
Returns

*Theviewthathasthegivenidinthehierarchyornull
因为是findViewById在是从View及其儿子中查找,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点或有相同的父亲节点,但不在父节点及以上节点调用findViewById通过id来查找他们就是没有问题。

一般我都是在layout的XML文件中设置View的id。
比如
<TextView
android:id="@+id/lable2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:editable="false"/>
<View
android:id="@+id/View01"android:layout_width="wrap_content"android:layout_height="wrap_content"></View>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics