Android使用addView动态加载布局文件
生活随笔
收集整理的這篇文章主要介紹了
Android使用addView动态加载布局文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
第一步,要獲取要將動(dòng)態(tài)加載內(nèi)容置入的容器?relativeLayout
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
第二步,獲得要?jiǎng)討B(tài)加載的布局文件
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.searchbox, null);
relativeLayout.addView(view);
第三步,動(dòng)態(tài)載入
relativeLayout.addView(view);
看完整示例:
btn.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
// TODO Auto-generated method stub
context = v.getContext();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.searchbox, null);
relativeLayout.addView(view);
ImageButton searchBoxBtn = (ImageButton) view
.findViewById(R.id.searchBoxBtn);
searchBoxBtn.setOnClickListener(new SearchBoxBtnOnClickListener(
relativeLayout, view, tv));
Toast.makeText(context, "searchBtn", Toast.LENGTH_SHORT).show();
}
});
總結(jié)
以上是生活随笔為你收集整理的Android使用addView动态加载布局文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 使用intent.put
- 下一篇: android sina oauth2.