Android TextView兩端對齊解決辦法
Android TextView兩端對齊解決辦法
今天遇到一個關(guān)于TextView文字兩端對齊其實(shí)方案,大家都知道原生控件是不能滿足我們的需求的,因此需要自定義View
下面看下效果圖

package com.example.VerticalMarqueeTextView.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;
/**
* Created by John on 2017/2/9.
*/
public class WordAlignTextView extends TextView {
private float textSize;
private float textLineHeight;
//頂部
private int top;
//y軸
private int y;
//線
private int lines;
//底部
private int bottom;
//右邊
private int right;
//左邊
private int left;
//線字
private int lineDrawWords;
private char[] textCharArray;
private float singleWordWidth;
//每個字符的空隙
private float lineSpacingExtra;
private boolean isFirst = true;
public WordAlignTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
initTextInfo();
return true;
}
});
}
public WordAlignTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public WordAlignTextView(Context context) {
this(context, null, 0);
}
public void initTextInfo() {
textSize = getTextSize();
//獲取線的高度
textLineHeight = getLineHeight();
left = 0;
right = getRight();
y = getTop();
// 要畫的寬度
int drawTotalWidth = right - left;
String text = getText().toString();
if (!TextUtils.isEmpty(text) && isFirst) {
textCharArray = text.toCharArray();
TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.density = getResources().getDisplayMetrics().density;
mTextPaint.setTextSize(textSize);
// 獲取單個單詞的的寬度
singleWordWidth = mTextPaint.measureText("一") + lineSpacingExtra;
// 每行可以放多少個字符
lineDrawWords = (int) (drawTotalWidth / singleWordWidth);
int length = textCharArray.length;
lines = length / lineDrawWords;
if ((length % lineDrawWords) > 0) {
lines = lines + 1;
}
first = false;
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
int totalHeight = (int) (lines*textLineHeight+textLineHeight*2 + getPaddingBottom()+getPaddingTop()+layoutParams.bottomMargin+layoutParams.topMargin);
setHeight(totalHeight);
}
}
@Override
protected void onDraw(Canvas canvas) {
bottom = getBottom();
int drawTotalLine = lines;
if(maxLine!=0&&drawTotalLine>maxLine){
drawTotalLine = maxLine;
}
for (int i = 0; i < drawTotalLine; i++) {
try {
int length = textCharArray.length;
int mLeft = left;
// 第i+1行開始的字符index
int startIndex = (i * 1) * lineDrawWords;
// 第i+1行結(jié)束的字符index
int endTextIndex = startIndex + lineDrawWords;
if (endTextIndex > length) {
endTextIndex = length;
y += textLineHeight;
} else {
y += textLineHeight;
}
for (; startIndex < endTextIndex; startIndex++) {
char c = textCharArray[startIndex];
// if (c == ' ') {
// c = '\u3000';
// } else if (c < '\177') {
// c = (char) (c + 65248);
// }
canvas.drawText(String.valueOf(c), mLeft, y, getPaint());
mLeft += singleWordWidth;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
int maxLine;
public void setMaxLines(int max){
this.maxLine = max;
}
public void setLineSpacingExtra(int lineSpacingExtra){
this.lineSpacingExtra = lineSpacingExtra;
}
/**
* 判斷是否為中文
* @return
*/
public static boolean containChinese(String string){
boolean flag = false;
for (int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
if ((c >= 0x4e00) && (c <= 0x9FA5)) {
flag = true;
}
}
return flag;
}
public static String ToDBC(String input) {
// 導(dǎo)致TextView異常換行的原因:安卓默認(rèn)數(shù)字、字母不能為第一行以后每行的開頭字符,因?yàn)閿?shù)字、字母為半角字符
// 所以我們只需要將半角字符轉(zhuǎn)換為全角字符即可
char c[] = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == ' ') {
c[i] = '\u3000';
} else if (c[i] < '\177') {
c[i] = (char) (c[i] + 65248);
}
}
return new String(c);
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
基于Flutter制作一個長按展示操作項(xiàng)面板的桌面圖標(biāo)
Flutter是一種強(qiáng)大的跨平臺移動應(yīng)用程序框架,它能夠幫助開發(fā)者輕松地創(chuàng)建漂亮、快速、高效的應(yīng)用程序,本文的主題是如何在Flutter中制作一個長按展示操作項(xiàng)面板的桌面圖標(biāo),在某些場景下,這個功能會讓應(yīng)用程序更加便利和易用2023-06-06
Android Moveview滑屏移動視圖類完整實(shí)例
這篇文章主要介紹了Android Moveview滑屏移動視圖類,很有實(shí)用價值,需要的朋友可以參考下2014-07-07
Android使用Handler實(shí)現(xiàn)下載文件功能
這篇文章主要為大家詳細(xì)介紹了Android使用Handler實(shí)現(xiàn)下載文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android開發(fā)中ViewPager實(shí)現(xiàn)多頁面切換效果
ViewPager用于實(shí)現(xiàn)多頁面的切換效果,該類存在于Google的兼容包里面,所以在引用時記得在BuilldPath中加入“Android-support-v4.jar”。具體詳情大家可以參考下本文2016-11-11
android判斷點(diǎn)擊位置是否在扇形區(qū)域內(nèi)
這篇文章主要為大家詳細(xì)介紹了android判斷點(diǎn)擊位置是否在扇形區(qū)域內(nèi),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android自定義View系列之Path繪制仿支付寶支付成功動畫
這篇文章主要為大家詳細(xì)介紹了Android自定義View系列之Path繪制仿支付寶支付成功動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android中ListView下拉刷新的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了Android中ListView下拉刷新的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-03-03

