Android重寫TextView實現(xiàn)文字整齊排版的方法(附demo源碼下載)
本文實例講述了Android重寫TextView實現(xiàn)文字整齊排版的方法。分享給大家供大家參考,具體如下:
XRTextView類
package rong.android.test;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
public class XRTextView extends TextView{
private final String namespace = "rong.android.TextView";
private String text;
private float textSize;
private float paddingLeft;
private float paddingRight;
private float marginLeft;
private float marginRight;
private int textColor;
private JSONArray colorIndex;
private Paint paint1 = new Paint();
private Paint paintColor = new Paint();
private float textShowWidth;
private float Spacing = 0;
private float LineSpacing = 1.3f;//行與行的間距
public XRTextView(Context context, AttributeSet attrs) {
super(context, attrs);
text = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/android", "text");
textSize = attrs.getAttributeIntValue(namespace, "textSize", 25);//字體大小
textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.BLUE);//字體顏色
paddingLeft = attrs.getAttributeIntValue(namespace, "paddingLeft", 0);
paddingRight = attrs.getAttributeIntValue(namespace, "paddingRight", 0);
marginLeft = attrs.getAttributeIntValue(namespace, "marginLeft", 0);
marginRight = attrs.getAttributeIntValue(namespace, "marginRight", 0);
paint1.setTextSize(textSize);
paint1.setColor(textColor);
paint1.setAntiAlias(true);
paintColor.setAntiAlias(true);
paintColor.setTextSize(textSize);
paintColor.setColor(Color.BLUE);
}
public XRTextView(Context context, float textSize, int textColor, float paddingLeft, float paddingRight, float marginLeft, float marginRight){
super(context);
this.textSize = textSize;
this.textColor = textColor;
this.paddingLeft = paddingLeft;
this.paddingRight = paddingRight;
this.marginLeft = marginLeft;
this.marginRight = marginRight;
paint1.setTextSize(textSize);
paint1.setColor(textColor);
paint1.setAntiAlias(true);
paintColor.setAntiAlias(true);
paintColor.setTextSize(textSize);
paintColor.setColor(Color.BLUE);
}
public JSONArray getColorIndex() {
return colorIndex;
}
public void setColorIndex(JSONArray colorIndex) {
this.colorIndex = colorIndex;
}
/**
* 傳入一個索引,判斷當前字是否被高亮
* @param index
* @return
* @throws JSONException
*/
public boolean isColor(int index) throws JSONException{
if(colorIndex == null){
return false;
}
for(int i = 0 ; i < colorIndex.length() ; i ++){
JSONArray array = colorIndex.getJSONArray(i);
int start = array.getInt(0);
int end = array.getInt(1)-1;
if(index >= start && index <= end){
return true;
}
}
return false;
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
View view=(View)this.getParent();
textShowWidth=view.getMeasuredWidth()-paddingLeft - paddingRight - marginLeft - marginRight;
int lineCount = 0;
text = this.getText().toString();//.replaceAll("\n", "\r\n");
if(text==null)return;
char[] textCharArray = text.toCharArray();
// 已繪的寬度
float drawedWidth = 0;
float charWidth;
for (int i = 0; i < textCharArray.length; i++) {
charWidth = paint1.measureText(textCharArray, i, 1);
if(textCharArray[i]=='\n'){
lineCount++;
drawedWidth = 0;
continue;
}
if (textShowWidth - drawedWidth < charWidth) {
lineCount++;
drawedWidth = 0;
}
boolean color = false;
try {
color = isColor(i);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(color){
canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
(lineCount + 1) * textSize * LineSpacing, paintColor);
}else{
canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
(lineCount + 1) * textSize * LineSpacing, paint1);
}
if(textCharArray[i] > 127 && textCharArray[i] != '、' && textCharArray[i] != ',' && textCharArray[i] != '。' && textCharArray[i] != ':' && textCharArray[i] != '!'){
drawedWidth += charWidth + Spacing;
}else{
drawedWidth += charWidth;
}
}
setHeight((int) ((lineCount + 1) * (int) textSize * LineSpacing + 10));
}
public float getSpacing() {
return Spacing;
}
public void setSpacing(float spacing) {
Spacing = spacing;
}
public float getMYLineSpacing() {
return LineSpacing;
}
public void setMYLineSpacing(float lineSpacing) {
LineSpacing = lineSpacing;
}
public float getMYTextSize() {
return textSize;
}
public void setMYTextSize(float textSize) {
this.textSize = textSize;
paint1.setTextSize(textSize);
paintColor.setTextSize(textSize);
}
}
MainActivity類
package rong.android.test;
import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private XRTextView xrtextview = null;
private TextView textview = null;
private String content = "abcdefgABCDEF我要你lfwjkdfl;skjf asljkflskjfls;kjfsljfwfisdlfjsllkjsdfjlskjf546132s1f3sd4f31s3dffslfksjdfljlsadkjflsajdf sdfjklsajdflsa;jdfls 的!@#$%^&*()_";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xrtextview = (XRTextView) this.findViewById(R.id.mytextview_tv);
xrtextview.setText(content);
textview = (TextView) this.findViewById(R.id.mytextview_tv1);
textview.setText(content);
}
}
布局文件
<LinearLayout 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:orientation="vertical" >
<rong.android.test.XRTextView
android:id="@+id/mytextview_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/mytextview_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />
</LinearLayout>
完整實例代碼點擊此處本站下載。
更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android Service組件使用技巧總結》、《Android基本組件用法總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- Android編程實現(xiàn)自動調整TextView字體大小以適應文字長度的方法
- Android TextView控件文字添加下劃線的實現(xiàn)方法
- Android TextView中文本點擊文字跳轉 (代碼簡單)
- Android TextView顯示html樣式的文字
- Android編程開發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)
- Android中EditText和AutoCompleteTextView設置文字選中顏色方法
- android顯示TextView文字的倒影效果實現(xiàn)代碼
- android Textview文字監(jiān)控(Textview使用方法)
- Android實現(xiàn)TextView中文字鏈接的4種方式介紹及代碼
- Android之TextView自適應大小
相關文章
使用SharedPreferences在Android存儲對象詳細代碼
這篇文章主要介紹了使用SharedPreferences在Android存儲對象并附上詳細代碼,下面文章內(nèi)容較少,大多以代碼的形式體現(xiàn),需要的小伙伴可以參考一下,希望對你有所幫助2021-11-11
android IntentService實現(xiàn)原理及內(nèi)部代碼分享
android IntentService實現(xiàn)原理及內(nèi)部代碼分享,需要的朋友可以參考一下2013-06-06
Android開發(fā)RecyclerView實現(xiàn)折線圖效果
這篇文章主要為大家詳細介紹了Android開發(fā)RecyclerView實現(xiàn)折線圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-09-09
Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析
這篇文章主要給大家介紹了關于Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Flutter具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04

