Android 动态加载布局文件
16lz
2021-01-23
setContentView(R.layout.activity_main); TextView text = (TextView)this.findViewById(R.id.DynamicText);text.setText("Hello World");
(2) 第二种方式,通过 LayoutInflater 来间接加载 ,即: LayoutInflater mInflater = LayoutInflater.from(this); View contentView = mInflater.inflate(R.layout.activity_main,null); TextView text = (TextView)contentView.findViewById(R.id.DynamicText);text.setText("Hello World"); setContentView(contentView);
LinearLayout layout = new LinearLayout(this); TextView text = new TextView(this);text.setText("Hello World");text.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); layout.addView(text); setContentView(layout);
更多相关文章
- android 工程里缺少 R.java 文件原因和解决方法
- 使用pull解析器操作xml文件
- 解决android一直在running,打不开文件,下载不了gradle包的方法
- 【android开发】解析xml文件①
- 巧用布局文件实现Android中实现事件监听机制