很多控件在onCreate()方法中获取到之后并不能马上获取到高度,原因很简单,控件还没有绘制完毕,那么应该在何种时机获取控件的高度,本文以TextView为例重点讨论一下,首先看一个小例子
首先在一个activity的布局中只放一个TextView,然后在布局中规定好文本,宽高都是“wrap_content”,布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context="com.example.administrator.measuretest.TestTextViewActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="60sp"
android:text="Hello World!" />
</RelativeLayout>
现在我们在onStart()方法中获取其高度
public class TestTextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_text_view);
}
@Override
protected void onStart() {
super.onStart();
TextView tv1 = (TextView) findViewById(R.id.tv1);
Log.i("Alex","getMeasuredHeight="+tv1.getMeasuredHeight());
Log.i("Alex","getMeasuredState="+tv1.getMeasuredState());
Log.i("Alex","getHeight="+tv1.getHeight());
Log.i("Alex","getWidth="+tv1.getWidth());
Log.i("Alex","getMeasuredWidth="+tv1.getMeasuredWidth());
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)tv1.getLayoutParams();
Log.i("Alex","layoutParams.height="+layoutParams.height+" layoutParams.width="+layoutParams.width);
}
}
08-08 16:01:20.489 7573-7573/com.example.administrator.measuretest I/Alex: getMeasuredHeight=0
08-08 16:01:20.489 7573-7573/com.example.administrator.measuretest I/Alex: getMeasuredState=0
08-08 16:01:20.489 7573-7573/com.example.administrator.measuretest I/Alex: getHeight=0
08-08 16:01:20.489 7573-7573/com.example.administrator.measuretest I/Alex: getWidth=0
08-08 16:01:20.489 7573-7573/com.example.administrator.measuretest I/Alex: getMeasuredWidth=0
08-08 16:01:20.489 7573-7573/com.example.administrator.measuretest I/Alex: layoutParams.height=-2 layoutParams.width=-2
我们发现,不管是getMeasuredHeight,布局的高度还是getHeight都是0,也就是说,getMeasuredHeight并没有进行测量。下面有这样几个解决方案
1、等控件绘制完毕之后进行测量
2、观察者模式
3、调用onMeasure()方法进行测量
首先看第1、2中方法,其实原理是一样的,都是等控件绘制完毕之后进行测量,代码如下
@Override
protected void onStart() {
super.onStart();
final TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1.post(new Runnable() {
@Override
public void run() {
Log.i("Alex","getMeasuredHeight="+tv1.getMeasuredHeight());
Log.i("Alex","getMeasuredState="+tv1.getMeasuredState());
Log.i("Alex","getHeight="+tv1.getHeight());
Log.i("Alex","getWidth="+tv1.getWidth());
Log.i("Alex","getMeasuredWidth="+tv1.getMeasuredWidth());
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)tv1.getLayoutParams();
Log.i("Alex","layoutParams.height="+layoutParams.height+" layoutParams.width="+layoutParams.width);
}
});
}
结果如下
08-08 16:05:13.301 10558-10558/com.example.administrator.measuretest I/Alex: getMeasuredHeight=161
08-08 16:05:13.302 10558-10558/com.example.administrator.measuretest I/Alex: getMeas