* 1、先建view对象 View view= View.inflate(this, R.layout.activity01, null); * 2、在view中填充R.layout.activity01页面 View view= View.inflate(this, R.layout.activity01, null); * 3、然后在view对象中添加各种控件(例如TextView,Button等),注意要转化成ViewGroup类型才可以添加 创建TextView控件 ((RelativeLayout)view).addView(textView); * 4、最后将view对象填充到页面,也就是把setContentView的值填充为view对象, setContentView(view);
* 1、创建TextView对象
TextView textView=new TextView(this);
* 2、给TextView对象设置布局参数
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(layoutParams);
* 3、给TextView对象设置其它属性
textView.setBackgroundColor(Color.CYAN);
textView.setText("你好,陌生人!!!");
package fry;
import com.fry.javaCreateView.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Activity01 extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setTitle("java代码创建view");
super.onCreate(savedInstanceState);
/*
* java代码创建view方法
* 1、先建view对象
* 2、在view中填充R.layout.activity01页面
* 3、然后在view对象中添加各种控件(例如TextView,Button等),注意要转化成ViewGroup类型才可以添加
* 4、最后将view对象填充到页面,也就是把setContentView的值填充为view对象,
*
* 创建控件方法,以TextView为例
* 1、创建TextView对象
* 2、给TextView对象设置布局参数
* 3、给TextView对象设置其它属性
*
*/
View view= View.inflate(this, R.layout.activity01, null);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
TextView textView=new TextView(this);
textView.setLayoutParams(layoutParams);
textView.setBackgroundColor(Color.CYAN);
textView.setText("你好,陌生人!!!");
((RelativeLayout)view).addView(textView);
setContentView(view);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有