亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

android如何獲取textview最多顯示

瀏覽:46日期:2022-09-18 11:50:19
方法一

工作中用的一個方法,雖然不算特別準確,但效果還是不錯的,這里分享下。

/** * 獲取textview最大能顯示幾個字 * @param text 文本內容 * @param size 文本字體大小 * @param maxWidth textview的最大寬度 * @return */ private float getLineMaxNumber(String text, float size,float maxWidth) {if (null == text || ''.equals(text)){ return 0;}Paint paint = new Paint();paint.setTextSize(size);//得到文本內容總體長度float textWidth = paint.measureText(text);// textWidthfloat width = textWidth / text.length();float total = maxWidth / width;return total; }

上面這個方法不太精確,不過比較適合在 RecyclerView 或 ListView 里面使用,避免生成太多對象

方法二

/** * 獲取textview一行最大能顯示幾個字(需要在TextView測量完成之后) * * @param text 文本內容 * @param paint textview.getPaint() * @param maxWidth textview.getMaxWidth()/或者是指定的數值,如200dp */ private int getLineMaxNumber(String text, TextPaint paint, int maxWidth) {if (null == text || ''.equals(text)) { return 0;}StaticLayout staticLayout = new StaticLayout(text, paint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);//獲取第一行最后顯示的字符下標return staticLayout.getLineEnd(0); }

利用 StaticLayout 可以非常輕松的得到一行可以顯示的最大字符數

延伸:對于一個單行 TextView,當字符串超出一行時,如何獲取未顯示的部分字符串?textview 設定最大行數為 1 后,文本超出了 textview,textView 末尾顯示省略號,我就想知道省略號代表的內容思路:假設 TextView 的寬度是在 xml 內設置的具體數值,比如 300dp,(目的是為了簡化這個問題,如果設置為 match_parent 或者 wrap_content,需要在程序運行時計算其寬度,而直接 getWidth 總是返回 0,比較麻煩。)比如是這樣配置的:

<TextViewandroid: 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,轉換成pxfloat 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轉Pxprivate 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.010-22 01:17:42.048: D/Text(21495): #0: I, width=8.0, sum=810-22 01:17:42.049: D/Text(21495): #1: f, width=9.0, sum=1710-22 01:17:42.049: D/Text(21495): #2: , width=7.0, sum=2410-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=21310-22 01:17:42.053: D/Text(21495): #18: , width=7.0, sum=22010-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=57510-22 01:17:42.061: D/Text(21495): #51: g, width=16.0, sum=59110-22 01:17:42.061: D/Text(21495): TextView shows #51 char: If you really want to hear about it, the first thin

到此這篇關于android獲取textview最多顯示的文章就介紹到這了,更多相關android textview最多顯示內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 特级毛片a级毛免费播放 | 97超级碰碰碰碰在线视频 | 亚洲另类视频在线观看 | 国产亚洲精品一区二区在线观看 | 中文字幕韩国 | 午夜在线视频一区二区三区 | 亚洲人成网站在线在线 | 日本人69视频在线观看 | 国产成人综合91精品 | 日韩欧美一区二区三区四区 | 一级女性全黄生活片免费看 | 国产成人综合在线观看网站 | 91精品网站 | 久碰香蕉精品视频在线观看 | 免费看一级| 操婷婷 | 亚洲十欧美十日韩十国产 | 亚洲精品三区 | 国产大片喷水在线在线视频 | 一级黄色免费观看 | 日本一级大黄毛片一级 | 在线免费看a | 99久久国产免费 - 99久久国产免费 | 国产视频自拍一区 | 国内精品自在自线在免费 | 成人深夜福利在线播放不卡 | 日本黄色影片在线观看 | 免费精品国产日韩热久久 | 麻豆传媒官网入口 | 久久国产自偷自免费一区100 | 黄p片| 女人被两根一起进3p在线观看 | 99久久免费国产香蕉麻豆 | 日韩免费一级a毛片在线播放一级 | 亚洲国产一区二区在线 | 中文岛国精品亚洲一区 | 成人精品综合免费视频 | 视频一本大道香蕉久在线播放 | 亚洲精品一区二区三区国产 | 欧美特黄特色aaa大片免费看 | 欧美黄色性 |