Android 获取textView高度的N种方式

本文探讨了在Android中如何在不同阶段获取TextView的高度,包括等待控件绘制完成、使用OnLayoutChangeListener、ViewTreeObserver等方法的优缺点,并提及主动测量的方式。在没有绘制完毕时,可通过StaticLayout预估文本行数和字符位置。
摘要由CSDN通过智能技术生成

很多控件在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

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值