获取textview的宽度

今天用到了记录下
当TextView的属性是wrap_content时,如何获取其宽度,特别是在界面还没显示时,比如像在RecycleView中的情况?

TextView tv_name = findViewById(R.id.tv_name); tv_name .setText(str);
int spec=View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
tv_name.measure(spec,spec);
int measuredWidthTicketNum = tv_name.getMeasuredWidth();

参考 https://blog.csdn.net/xiaoshuang516/article/details/51510361

你可能感兴趣的:(获取textview的宽度)