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 // 这里能获取真实的高和宽 } });
相关推荐
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:layout_width="match_parent" android:layout_height="wrap_content" TagsEditText:allowSpaceInTag="true" TagsEditText:tagsCloseImageRight="@drawable/tag_close" TagsEditText:tagsBackground="@...
android 可移动控件 /** * 用于判断view的移动 * @param view * @param event * @return */ @Override public boolean onTouch(View view, MotionEvent event) { final int X = (int) event.getRawX(); ...
Android支持多屏幕机制即用为当前设备屏幕提供一种合适的方式来共同管理并解析应用资源。本文就介绍了4中Android屏幕自适应解决方案。...传统的layout_weight使用方法是将当前控件的layout_width和layout_
android:layout_width和android:layout_height用于设置控件的宽和高,match_parent表示匹配父容器,wrap_content则是与内容相适应。 android:text用于设置文字的内容 android:textColor用于设置文字的颜色 android:...
StickerView是Android上的一个可以进行添加,缩放,拖动,删除操作的即时贴控件。如图所示:用法:StickerView 是基于 ImageView 布局的扩展。 android:id="@ id/sticker_view" android:layout_width=...
为什么有时候像android:layout_marginBottom等变量的赋值为负数? 例如如下代码: android:orientation="vertical" android:id="@id/Widget_2X4_frame" android:layout_width="fill_parent" android:...
android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示 --> <FrameLayout android...
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控件的展示 <?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-Chat-Widget 是像微信、WhatsApp、Line一样的聊天控件。 Demo 如何使用? 1.在Layout中 android:id="@ id/messageInputToolBox" android:layout_width="match_parent" ...
当我们将控件的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=...
android:layout_width="0dp" android:layout_height="match_parent"/> android:onClick="scrollTo3" android:background="@android:color/holo_green_light" android:text="NUM3" android:gravity=...
利用布局安排各种控件,设计良好用户界面 "<LinearLayout " "xmlns:android="schemas.android./apk/res/android" " "android:orientation="vertical" " "android:layout_width="fill_parent" " "android:layout_...
小编在此之前给大家介绍过关于Android自定义控件的用法等,需要的可以参考下:...它不是有layout_width和layout_height这两个属性吗?其实,这两个属性就是官方定义的 官方的操作其实就是继承了一个ViewGroup,Linear
android布局和控件 LinearLayout:线性布局,从左到右 第一种方法实现界面布局:手动写布局 第二种方法实现界面布局:布局文件(layout中的activty_main.xml) 其实就是标签的嵌套,注意单标签、双标签 拨打电话的...
一、简介 ...android:layout_width match_parent android:layout_height=wrap_content android:progress> 2、用ProgressBar对象指向ProgressBar控件 private ProgressBar pb_progressBar1; pb_pro
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