android 获取控件大小

在获取控件大小的时候,如果控件没有measure过.获取的大小就会是0.

设置控件的属性为invisible或者为gone时,如过经过measure过,同样也会获取到控件的大小.

 

visibleTextView = (TextView)findViewById(R.id.invi);
        goneTextView = (TextView)findViewById(R.id.gone);
        visibleTextView.setVisibility(View.GONE);
        goneTextView.setVisibility(View.INVISIBLE);

 

/*
	 * 获取控件宽
	 */
	public static int getWidth(View view) {
		int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		view.measure(w, h);
		return (view.getMeasuredWidth());
	}
	
	/*
	 * 获取控件高
	 */
	public static int getHeight(View view) {
		int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		view.measure(w, h);
		return (view.getMeasuredHeight());
	}

 

 

 

你可能感兴趣的:(android)