`
thierry.xing
  • 浏览: 655971 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
580fa9c1-4a0c-3f40-a55a-c9256ce73302
Sencha Touch中...
浏览量:0
社区版块
存档分类
最新评论

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

 
阅读更多

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

 

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

 

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

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设置监听器:

		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
	                       // 这里能获取真实的高和宽
			}
		});

 

2
1
分享到:
评论
2 楼 xiangxm 2013-11-06  
有关问题: 我自定义了两个控件一个是radiobutton 一个是Linearlayout  我想获取他们的,但是debug看到这两个控件的监听里面的参数在同时变化而且好像会执行>=2次,第二次会空指针异常崩溃。why》
1 楼 xiangxm 2013-11-06  
谢谢分享、谢谢分享、谢谢分享、

相关推荐

    如何让安卓(Android)子控件超出父控件的范围显示

    android:layout_width=match_parent android:layout_height=match_parent android:clipChildren=false> <ImageView android:layout_width=match_parent android:layout_height=match_parent an

    Android Tag 带删除控件

    android:layout_width="match_parent" android:layout_height="wrap_content" TagsEditText:allowSpaceInTag="true" TagsEditText:tagsCloseImageRight="@drawable/tag_close" TagsEditText:tagsBackground="@...

    android 可移动控件

    android 可移动控件 /** * 用于判断view的移动 * @param view * @param event * @return */ @Override public boolean onTouch(View view, MotionEvent event) { final int X = (int) event.getRawX(); ...

    4种Android屏幕自适应解决方案

    Android支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源。本文就介绍了4中Android屏幕自适应解决方案。...传统的layout_weight使用方法是将当前控件的layout_width和layout_

    Android程序技术:常用控件.pptx

    android:layout_width和android:layout_height用于设置控件的宽和高,match_parent表示匹配父容器,wrap_content则是与内容相适应。 android:text用于设置文字的内容 android:textColor用于设置文字的颜色 android:...

    Android即时贴控件StickerView.zip

    StickerView是Android上的一个可以进行添加,缩放,拖动,删除操作的即时贴控件。如图所示:‍用法:‍StickerView 是基于 ImageView 布局的扩展。   android:id="@ id/sticker_view"  android:layout_width=...

    androidlayout-marginBottom的值为负数.docx

    为什么有时候像android:layout_marginBottom等变量的赋值为负数? 例如如下代码:  android:orientation="vertical"  android:id="@id/Widget_2X4_frame"  android:layout_width="fill_parent"  android:...

    Android控件大全以及各布局空间的使用方式

    android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示 --> <FrameLayout android...

    Android抽屉控件VerticalDrawerLayout.zip

     android:layout_width="match_parent"  android:layout_height="match_parent">    android:id="@ id/content"  android:layout_width="match_parent"  android:layout_height="match_parent"  ...

    android中ratingbar的简单使用

    android控件的展示 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_...

    Android聊天控件Android-Chat-Widget.zip

    Android-Chat-Widget 是像微信、WhatsApp、Line一样的聊天控件。 Demo 如何使用? 1.在Layout中  android:id="@ id/messageInputToolBox"  android:layout_width="match_parent"  ...

    android自定义控件-侧滑菜单

    当我们将控件的layout_width或layout_height指定为具体数值时如andorid * :layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。 * * * MeasureSpec.AT_MOST是...

    省市区/县,详细地址选择自定义控件

    android:layout_width="match_parent" android:layout_height="wrap_content"/> listEditTextView=(ListEditTextView)findViewById(R.id.test_edit); listEditTextView.setListEditView(60,R.array.prompt_list,R...

    安卓在系统控件中加入自定义属性

    android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="20dp" android:layout_gravity="center" android:src="@mipmap/sweet" discrollve:discrollve_scaleX=...

    wkp111_StickLayout-粘性控件,其任意一个子控件都可滑动停留,本质为NestedScrollView和LinearLayout的结合。.zip

     android:layout_width="0dp"  android:layout_height="match_parent"/>    android:onClick="scrollTo3"  android:background="@android:color/holo_green_light"  android:text="NUM3"  android:gravity=...

    Android实验指导.doc

    利用布局安排各种控件,设计良好用户界面 "<LinearLayout " "xmlns:android="schemas.android./apk/res/android" " "android:orientation="vertical" " "android:layout_width="fill_parent" " "android:layout_...

    实例讲解Android自定义控件

    小编在此之前给大家介绍过关于Android自定义控件的用法等,需要的可以参考下:...它不是有layout_width和layout_height这两个属性吗?其实,这两个属性就是官方定义的   官方的操作其实就是继承了一个ViewGroup,Linear

    android基础

    android布局和控件 LinearLayout:线性布局,从左到右 第一种方法实现界面布局:手动写布局 第二种方法实现界面布局:布局文件(layout中的activty_main.xml) 其实就是标签的嵌套,注意单标签、双标签 拨打电话的...

    Android进度条控件progressbar使用方法详解

    一、简介 ...android:layout_width match_parent android:layout_height=wrap_content android:progress> 2、用ProgressBar对象指向ProgressBar控件 private ProgressBar pb_progressBar1; pb_pro

    Android代码-一个安卓正方形ImgeView控件

    SquareImageView English on the bottom 一个安卓正方形ImgeView控件 使用方式 1:导入SqueraImageView到您的项目 2:在XML文件中,设置...2: setting the width to a value, and height to any value on the XML fi

Global site tag (gtag.js) - Google Analytics