Rumah  >  Soal Jawab  >  teks badan

android - 对于一个单行TextView,当字符串超出一行时,如何获取未显示的部分字符串?

PHP中文网PHP中文网2718 hari yang lalu373

membalas semua(1)saya akan balas

  • 大家讲道理

    大家讲道理2017-04-17 14:36:53

    假设TextView的宽度是在xml内设置的具体数值,比如300dp,
    (目的是为了简化这个问题,如果设置为match_parent或者wrap_content,需要在程序运行时计算其宽度,而直接getWidth总是返回0,比较麻烦。)

    比如是这样配置的:

        <TextView
            android:id="@+id/textView"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:singleLine="true" />

    然后填充了一个超长的字符串,比如这样:

    String str = "If you really want to hear about it, the first thing you'll probably want to know";

    这样就会导致显示不全,像这样:
    If you really want to hear about it, the first thin...

    所以,如果你想得到已显示的字符个数,或者未显示的字符个数,那么其中的关键是如何计算每一个字符的宽度
    然后遍历这个字符串,当前n个字符宽度总和,超过TextView宽度时,就得到了已显示的字符个数。

    String str = "If you really want to hear about it, the first thing you'll probably want to know";
    mTextView = (TextView) findViewById(R.id.textView);
    
    // 计算TextView宽度:xml中定义的宽度300dp,转换成px
    float textViewWidth = convertDpToPixel(300);
    float dotWidth = getCharWidth(mTextView, '.');
    Log.d(TAG, "TextView width " + textViewWidth);
    
    int sumWidth = 0;
    for (int index=0; index<str.length(); index++) {
        // 计算每一个字符的宽度
        char c = str.charAt(index);
        float charWidth = getCharWidth(mTextView, c);
        sumWidth += charWidth;
        Log.d(TAG, "#" + index + ": " + c + ", width=" + charWidth + ", sum=" + sumWidth);
        
        if (sumWidth + dotWidth*3 >= textViewWidth) {
            Log.d(TAG, "TextView shows #" + index + " char: " + str.substring(0, index));
            break;
        }
    }
    
    // Dp转Px
    private float convertDpToPixel(float dp){
        Resources resources = getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return px;
    }
    
    // 计算每一个字符的宽度
    public float getCharWidth(TextView textView, char c) {
        textView.setText(String.valueOf(c));
        textView.measure(0, 0);
        return textView.getMeasuredWidth();
    }  

    结果如下,在荣耀3C和LG G3上测试通过(G3比计算的结果,多显示了一个字符)

    10-22 01:17:42.046: D/Text(21495): TextView width 600.0
    10-22 01:17:42.048: D/Text(21495): #0: I, width=8.0, sum=8
    10-22 01:17:42.049: D/Text(21495): #1: f, width=9.0, sum=17
    10-22 01:17:42.049: D/Text(21495): #2:  , width=7.0, sum=24
    10-22 01:17:42.049: D/Text(21495): #3: y, width=14.0, sum=38
    ......
    10-22 01:17:42.053: D/Text(21495): #17: t, width=9.0, sum=213
    10-22 01:17:42.053: D/Text(21495): #18:  , width=7.0, sum=220
    10-22 01:17:42.053: D/Text(21495): #19: t, width=9.0, sum=229
    ......
    
    10-22 01:17:42.061: D/Text(21495): #50: n, width=16.0, sum=575
    10-22 01:17:42.061: D/Text(21495): #51: g, width=16.0, sum=591
    10-22 01:17:42.061: D/Text(21495): TextView shows #51 char: If you really want to hear about it, the first thin

    就是这样。

    balas
    0
  • Batalbalas