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: 要在Android获取TextView的文本,可以使用以下代码: ```java TextView textView = findViewById(R.id.textView); // 获取TextView对象 String text = textView.getText().toString(); // 获取TextView的文本 ``` 其中,`findViewById(R.id.textView)`用于获取布局文件中的TextView对象,`getText()`方法用于获取TextView的文本,`toString()`方法用于将文本转换为字符串类型。 ### 回答2: 在Android中,获取TextView的文本是一项非常常见和基本的任务,可以使用以下方法实现: 1. getText()方法 TextView中有一个getText()方法,可以返回TextView的文本内容,返回值类型为CharSequence类型。例如: //获取TextView对象 TextView textView = findViewById(R.id.text_view); //获取TextView的文本 CharSequence text = textView.getText(); 2. getText().toString()方法 getText().toString()方法可以将TextView的文本内容直接转换成String类型。例如: //获取TextView对象 TextView textView = findViewById(R.id.text_view); //获取TextView的文本并转换成字符串类型 String text = textView.getText().toString(); 3. getText().toString().trim()方法 在获取TextView文本内容时,有时候文本内容前后可能会存在空格或者换行符,这时候可以使用trim()方法去掉这些无用字符。例如: //获取TextView对象 TextView textView = findViewById(R.id.text_view); //获取TextView的文本并去掉无用字符 String text = textView.getText().toString().trim(); 以上就是获取TextView文本的几方式,开发者可以根据实际需求选择适合自己的方法。值得注意的是,在获取文本内容之前一定要确保TextView已经被初始化并设置了内容,否则可能会引发NullPointerException异常。 ### 回答3: 在 Android 应用中,TextView是一常见的UI组件,通常用于显示卡片内容,标题,按钮标签以及其它文本信息。因此,有时我们需要从 TextView获取其文本内容以进行一些操作,例如保存到数据库,发送给服务器,或者对它进行处理和分析。下面是获取 TextView 文本的几方法: 方法一:使用 getText() 方法 使用 TextView 的 getText() 方法可以直接获取它的文本内容,如下所示: TextView textView = findViewById(R.id.myTextView); String text = textView.getText().toString(); 这里我们找到textview对象,然后调用getText()方法,最后将其转化为字符串就可以获得textview中的文本内容。 方法二:使用 getResources() 和 getIdentifier() 方法 通过 getResources() 和 getIdentifier() 方法可以获取TextView 的文本内容,但是这方法比较复杂,适用于当我们不知道具体的 TextView 对象时。示例如下: Resources res = getResources(); int resId = res.getIdentifier("myTextView", "id", getPackageName()); TextView textView = findViewById(resId); String text = textView.getText().toString(); 这里我们通过先获取资源,然后通过 getIdentifier() 方法获取TextView 的 ID,再通过 findViewById() 找到 TextView 对象,最后调用 getText() 方法获取其文本内容并转换成字符串。 方法三:使用通过 EditText 获取文本内容 当 TextView 对象是不可编辑的时候,可以将其转换为 EditText 对象获取其文本内容。示例如下: TextView textView = findViewById(R.id.myTextView); EditText editText = new EditText(this); editText.setText(textView.getText().toString()); String text = editText.getText().toString(); 这里我们首先找到 TextView 对象,然后创建一个 EditText 对象,设置其内容为 TextView 的文本内容,最后调用 getText() 方法获取 EditText 的文本内容并转换成字符串。 综上所述,以上是三获取 TextView 文本的方法,开发者可以根据具体应用场景选择最合适的方式。具体选择哪方法需要结合具体的业务场景来判断。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值