Android自定义控件中加载layout XML文件

    对于通用的控件,常常单独提取出来。被其他的布局文件引用,可在布局文件中设置该控件的一些属性值,或直接在代码中修改该控件的值。对于通用的控件,一般可以直接在布局文件中定义,代码实现时继承ViewGroup或RelativeLayout之类。在构造函数中,像下面代码中加载该布局文件即可。加载完布局文件,可对一些布局里的控件进行赋值。
    
	public Titlebar(Context context, AttributeSet attrs) {
		super(context, attrs);
		[b]View.inflate(context, R.layout.title2, this);[/b]
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.Titlebar);
		String text = typedArray.getString(R.styleable.Titlebar_titleText);
		titleText = (TextView) findViewById(R.id.contentTitle);
		titleText.setText(text);
	}
    

猜你喜欢

转载自godjohnny.iteye.com/blog/2231111