1.調(diào)用res下的drawable或string資源對(duì)xml布局進(jìn)行設(shè)定的方法: //get Resources Resources rs = getBaseContext().getResources(); //getDrawable Drawable dr = rs.getDrawable(R.drawable.icon); mText.setBackgroundDrawable(dr); //getString CharSequence cs = rs.getString(R.string.str); mText.setText(cs);
2.LayoutInflator (布局?jǐn)U展) 通常,用findViewById在xml layout中找控件,而inflate則是從res/layout下找到xml布局文件,并把xml表述的layout轉(zhuǎn)化為View。比如,在代碼中需用到setView(v),而這個(gè)v卻是一個(gè)res/layout下的布局文件,這時(shí)就需要用到LayoutInflator了。 //LayoutInflater inflater = getLayoutInflater(); 這種方法也可以。 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.custom_dialog, null); ... ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.icon); ... builder.setView(layout);
3.listView Adapter帶按鈕 ListView條目中有按鈕控件時(shí),若不將按鈕的clickable的屬性設(shè)置為false,遇ListView會(huì)看不到選中的效果來(lái).另外,或在條目布局中設(shè)置了背景,則ListView也不能看到選中的效果.
作者:savant-pan, 微博 http://weibo.com/panxuewen,歡迎交指正、交流。
|