关于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中

publicfinalView findViewById (intid)
Since:APILevel1

Lookforachildviewwiththegivenid.Ifthisviewhasthegivenid,returnthisview.
Parameters
id Theidtosearchfor.
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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值