Android解决获取控件Width和Height为0的问题

Android在onCreate方法中调用View的getWidth和getHeight时返回的结果是0,因为此时控件还没有Draw出来,所以长和宽都是0。

如果想得到Width和Height,可以对View进行监听:

首先需要重写View的onSizeChange方法,加入监听回调函数:

Java代码 复制代码 收藏代码
  1. public class MyTextView extends TextView {
  2. public MyTextView(Context context, AttributeSet attrs, int defStyle) {
  3. super(context, attrs, defStyle);
  4. // TODO Auto-generated constructor stub
  5. }
  6. public MyTextView(Context context, AttributeSet attrs) {
  7. super(context, attrs);
  8. // TODO Auto-generated constructor stub
  9. }
  10. public MyTextView(Context context) {
  11. super(context);
  12. // TODO Auto-generated constructor stub
  13. }
  14. SizeChangeListener l;
  15. public void setSizeChangeListener(SizeChangeListener orlExt) {
  16. l = orlExt;
  17. }
  18. @Override
  19. public void onSizeChanged(int w, int h, int oldw, int oldh) {
  20. // TODO Auto-generated method stub
  21. l.sizeChanged(w, h, oldw, oldh);
  22. super.onSizeChanged(w, h, oldw, oldh);
  23. }
  24. public interface SizeChangeListener {
  25. public void sizeChanged(int w, int h, int oldw, int oldh);
  26. }
  27. }
public class MyTextView extends TextView {

	public MyTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public MyTextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	SizeChangeListener l;

	public void setSizeChangeListener(SizeChangeListener orlExt) {
		l = orlExt;
	}

	@Override
	public void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		l.sizeChanged(w, h, oldw, oldh);
		super.onSizeChanged(w, h, oldw, oldh);
	}

	public interface SizeChangeListener {
		public void sizeChanged(int w, int h, int oldw, int oldh);
	}
}

然后在Activity中对View设置监听器:

Java代码 复制代码 收藏代码
  1. tv_front = new MyTextView(this);
  2. tv_front.setBackgroundResource(R.drawable.slidebar);
  3. tv_front.setTextColor(Color.BLACK);
  4. tv_front.setText("要闻");
  5. tv_front.setGravity(Gravity.CENTER);
  6. tv_front.setTextSize(16);
  7. //监听textview尺寸改变
  8. tv_front.setSizeChangeListener(new SizeChangeListener() {
  9. @Override
  10. public void sizeChanged(int w, int h, int oldw, int oldh) {
  11. // TODO Auto-generated method stub
  12. // 这里能获取真实的高和宽
  13. }
  14. });
		tv_front = new MyTextView(this);
		tv_front.setBackgroundResource(R.drawable.slidebar);
		tv_front.setTextColor(Color.BLACK);
		tv_front.setText("要闻");
		tv_front.setGravity(Gravity.CENTER);
		tv_front.setTextSize(16);
		//监听textview尺寸改变
		tv_front.setSizeChangeListener(new SizeChangeListener() {
			
			@Override
			public void sizeChanged(int w, int h, int oldw, int oldh) {
				// TODO Auto-generated method stub
	                       // 这里能获取真实的高和宽
			}
		});

转载于:https://www.cnblogs.com/shhaoran/archive/2013/02/07/2924504.html

你可能感兴趣的:(Android解决获取控件Width和Height为0的问题)