控件的id是自己在xml中定義的, 可以通過R.id.來得到, 如果是view的話,可以直接調(diào)用view.getId()即可得到該ID了

西湖網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)公司2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
layout為布局,布局里面可以放任何空間,獲取空間可以用findViewById方法獲取
android 獲取某個(gè)布局控件 添加到另一個(gè)布局中
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.layout456);
ImageView imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.ic_launcher);
relativeLayout.addView(imgApple2);
LayoutInflater factorys = LayoutInflater.from(MainActivity.this);
final View textEntryView = factorys.inflate(R.layout.layout1, null);
// LinearLayout linearLayout = (LinearLayout) textEntryView
// .findViewById(R.id.layout1);
// relativeLayout.addView(linearLayout);
EditText editText1 = (EditText) textEntryView
.findViewById(R.id.editText1);
relativeLayout.addView(editText1);
在實(shí)際開發(fā)中LayoutInflater這個(gè)類還是非常有用的,它的作用類似于findViewById()。不同點(diǎn)是LayoutInflater是用來找res/layout/下的xml布局文件代碼塊,并且實(shí)例化;而findViewById()是找xml布局文件下的具體widget控件(如Button、TextView等)。 具體作用:
1、對于一個(gè)沒有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate()來載入;
2、對于一個(gè)已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。
LayoutInflater 是一個(gè)抽象類,在文檔中如下聲明:
public abstract class LayoutInflater extends Object
獲得 LayoutInflater 實(shí)例的三種方式:
1.LayoutInflater inflater = getLayoutInflater(); //調(diào)用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1. LayoutInflater inflater = LayoutInflater.from(context);
其實(shí),這三種方式本質(zhì)是相同的,從源碼中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是調(diào)用 PhoneWindow 的getLayoutInflater()方法,看一下該源代碼:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其實(shí)是調(diào)用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater ==null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其實(shí)調(diào)用 context.getSystemService()。
結(jié)論:所以這三種方式最終本質(zhì)是都是調(diào)用的Context.getSystemService()。
inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
public View inflate (int resource, ViewGroup root);
3 public View inflate (XmlPullParser parser, ViewGroup root);
4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);
5 public View inflate (int resource, ViewGroup root, boolean attachToRoot);
6
7 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
8 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
9 //EditText editText = (EditText)findViewById(R.id.content);
10 // error
EditText editText = (EditText)view.findViewById(R.id.content);
對于上面代碼,指定了第二個(gè)參數(shù) ViewGroup root,當(dāng)然你也可以設(shè)置為 null 值。
注意:
·inflate方法與 findViewById 方法不同;
·inflater 是用來找 res/layout下的 xml 布局文件,并且實(shí)例化;
·findViewById() 是找具體 xml 布局文件中的具體 widget 控件(如:Button、TextView 等)。
本文標(biāo)題:android獲取控件,android獲取控件AttributeSet
本文URL:http://chinadenli.net/article31/dsspesd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、商城網(wǎng)站、網(wǎng)站制作、品牌網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)