inflate()和findViewById(),findviewbyid
1.findViewById()是View對象的方法,先通過inflate()方法得到View,調用這個View對象的getViewById()方法,就能得到這個View樹上的子View。
2.inflate()函數可以通過getSystemService()方法得到,參數是LAYOUT_INFLATER_SERVICE。
3.inflate()函數的第一個參數是布局檔案的R引用,第二個參數是根 ViewGroup,也就是從哪個ViewGroup節點開始渲染。對於第二個參數,需要執行個體化這個VIewGroup,可以這麼做,在XML布局檔案中設定這個root的id,然後用findViewById()方法通過id就能得到它,比如
- View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup)
- findViewById(R.id.toast_layout_root));
android findViewByid方法
不能獲得buttonB
各位大俠幫忙呀 為何Viewinflate報null 指標錯誤
額 解決了 用了一個適配器裡用過的方法 我無非是想得到一個progressbar的列表 用這方法可以得到,得到了就能做很多事了 嘿嘿 android new progressbar預設的樣式是圓形的 網上講的什麼反射機制去改變某個屬性來實現 我覺得對我這種初學者很麻煩 就用這個方法吧linear.addView(txt,params); View view =View.inflate(context,R.layout.bookprogressbar, null); if(view.findViewById(R.id.bookprogressbar) instanceof ProgressBar){ progressbar=(ProgressBar)view.findViewById(R.id.bookprogressbar); progressbar.setTag(xxxxx); progresslist.add(progressbar); progressbar.setProgress(Integer.parseInt(xxxxx); map.put("progressbar", progressbar); linear.addView(view,params); }