关于findViewById

来源:互联网 发布:找人设计淘宝店多少钱 编辑:程序博客网 时间:2024/05/16 01:26
android.app.Activity中
public View findViewById (int id)
Since: API Level 1

Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Returns

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

android.view.View中

public final View findViewById (int id)
Since: API Level 1

Look for a child view with the given id. If this view has the given id, return this view.
Parameters
id     The id to search for.
Returns

    * The view that has the given id in the hierarchy or null 
因为是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>
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机来电被拦截怎么办 手机被加黑名单怎么办 手机短信被屏蔽了怎么办 手机信息被拦截怎么办 手机有感染病毒怎么办 华为p10速度慢怎么办 华为手机清理慢 怎么办 华为手机有回音怎么办 手机没有返回键怎么办 手机总显示内存不足怎么办 扩音器耳麦接触不良怎么办 音响插头接触不良怎么办 华为手机耳机声音小怎么办 苹果耳机孔变形怎么办 耳机孔松了怎么办 荣耀9青春版费电怎么办 华为双清了怎么办 华为开不开机怎么办 华为手机黑屏打不开怎么办 荣耀v8指纹消失怎么办 耳机话筒进水了怎么办 beats耳机进水了怎么办 音量孔进水了怎么办 苹果手机屏幕进水了怎么办 耳机孔进水了怎么办 华为v10声音小怎么办 荣耀v10声音小怎么办 华为变耳机模式怎么办 小米六耳机模式怎么办 苹果成耳机模式怎么办 苹果调耳机模式怎么办 ipad耳机孔进水怎么办 iphone一直是耳机模式怎么办 华为耳机音质不好怎么办 苹果声音键失灵怎么办 苹果耳机不兼容怎么办 usb耳机声音小怎么办 手机不认耳机怎么办 华为手机视频无声怎么办 苹果进入耳机模式怎么办 号码丢了怎么办挂失