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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

android如何獲取textview最多顯示

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

工作中用的一個(gè)方法,雖然不算特別準(zhǔn)確,但效果還是不錯(cuò)的,這里分享下。

/** * 獲取textview最大能顯示幾個(gè)字 * @param text 文本內(nèi)容 * @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);//得到文本內(nèi)容總體長(zhǎng)度f(wàn)loat textWidth = paint.measureText(text);// textWidthfloat width = textWidth / text.length();float total = maxWidth / width;return total; }

上面這個(gè)方法不太精確,不過(guò)比較適合在 RecyclerView 或 ListView 里面使用,避免生成太多對(duì)象

方法二

/** * 獲取textview一行最大能顯示幾個(gè)字(需要在TextView測(cè)量完成之后) * * @param text 文本內(nèi)容 * @param paint textview.getPaint() * @param maxWidth textview.getMaxWidth()/或者是指定的數(shù)值,如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);//獲取第一行最后顯示的字符下標(biāo)return staticLayout.getLineEnd(0); }

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

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

<TextViewandroid: android:layout_width='300dp'android:layout_height='wrap_content'android:ellipsize='end'android:singleLine='true' />

然后填充了一個(gè)超長(zhǎng)的字符串,比如這樣:

String str = 'If you really want to hear about it, the first thing you’ll probably want to know';這樣就會(huì)導(dǎo)致顯示不全,像這樣:If you really want to hear about it, the first thin...所以,如果你想得到已顯示的字符個(gè)數(shù),或者未顯示的字符個(gè)數(shù),那么其中的關(guān)鍵是如何計(jì)算每一個(gè)字符的寬度。然后遍歷這個(gè)字符串,當(dāng)前n個(gè)字符寬度總和,超過(guò)TextView寬度時(shí),就得到了已顯示的字符個(gè)數(shù)。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);// 計(jì)算TextView寬度:xml中定義的寬度300dp,轉(zhuǎn)換成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++) { // 計(jì)算每一個(gè)字符的寬度 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轉(zhuǎn)Pxprivate float convertDpToPixel(float dp){ Resources resources = getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float px = dp * (metrics.densityDpi / 160f); return px;}// 計(jì)算每一個(gè)字符的寬度public float getCharWidth(TextView textView, char c) { textView.setText(String.valueOf(c)); textView.measure(0, 0); return textView.getMeasuredWidth();}

結(jié)果如下,在榮耀 3C 和 LG G3 上測(cè)試通過(guò)(G3 比計(jì)算的結(jié)果,多顯示了一個(gè)字符):

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

到此這篇關(guān)于android獲取textview最多顯示的文章就介紹到這了,更多相關(guān)android textview最多顯示內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 免费国产草莓视频在线观看黄 | 日韩大尺度无遮挡理论片 | 亚洲高清美女一区二区三区 | 狠狠色噜噜狠狠狠狠2022 | 欧美一级在线 | 韩国19禁青草福利视频在线 | 欧美+日本+国产+在线观看 | 国产综合亚洲欧美日韩一区二区 | 黄色一级视频网站 | 伊人蕉久影院 | 欧美高清a | 网站污污 | 91精品免费观看 | 国产欧美成人一区二区三区 | 国产成人亚洲精品91专区手机 | 中文字幕第98页小明免费 | 亚洲精品免费在线 | 日本xxx片免费高清在线 | 老年人黄色一级片 | 久久vs国产综合色大全 | 国产精品午夜激爽毛片 | 亚洲午夜久久久久久91 | 中文字幕色在线 | 高清欧美在线三级视频 | 黄色链接在线观看 | 国产精品综合久成人 | 黄色一级免费 | 国产精品人成人免费国产 | 日韩一区二区三区在线观看 | 欧美国产精品久久 | 九一视频在线 | 国产91精品黄网在线观看 | 亚洲第一a | 色综合91久久精品中文字幕 | 网友自拍视频在线 | 日韩字幕在线 | 国产精品公开免费视频 | 日日夜操| 午夜成年免费观看视频 | 香蕉视频在线观看国产 | 午夜男人视频 |