欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android重寫TextView實(shí)現(xiàn)文字整齊排版的方法(附demo源碼下載)

 更新時(shí)間:2016年02月18日 10:57:22   作者:xurong  
這篇文章主要介紹了Android重寫TextView實(shí)現(xiàn)文字整齊排版的方法,結(jié)合實(shí)例形式分析了Android重寫TextView實(shí)現(xiàn)文字整齊排版的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下

本文實(shí)例講述了Android重寫TextView實(shí)現(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;
 }
 /**
 * 傳入一個(gè)索引,判斷當(dāng)前字是否被高亮
 * @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>

完整實(shí)例代碼點(diǎn)擊此處本站下載。

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android Service組件使用技巧總結(jié)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android仿Win8界面開發(fā)

    Android仿Win8界面開發(fā)

    這篇文章主要介紹了Android仿Win8界面開發(fā)的實(shí)例代碼,將要模仿的Win8界面的一個(gè)個(gè)設(shè)計(jì),分割成一個(gè)一個(gè)的方塊,感興趣的小伙伴們可以參考一下
    2015-12-12
  • 實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件

    實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件

    AutoCompleteTextView組件被用在輸入框中能實(shí)現(xiàn)輸入內(nèi)容自動(dòng)補(bǔ)全的功能,類似于大家平時(shí)用Google時(shí)的輸入聯(lián)想,這里我們來用實(shí)例講解Android中的AutoCompleteTextView自動(dòng)補(bǔ)全組件,特別是實(shí)現(xiàn)郵箱地址補(bǔ)全的例子,非常實(shí)用
    2016-05-05
  • Android漲姿勢知識(shí)點(diǎn)之你沒用過的BadgeDrawable

    Android漲姿勢知識(shí)點(diǎn)之你沒用過的BadgeDrawable

    現(xiàn)在Android中有許多的應(yīng)用仿蘋果的在應(yīng)用圖標(biāo)上顯示小紅點(diǎn),下面這篇文章主要給大家介紹了關(guān)于Android漲姿勢知識(shí)點(diǎn)之你沒用過的BadgeDrawable的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Android自定義照相機(jī)詳解

    Android自定義照相機(jī)詳解

    幾乎每個(gè)APP都會(huì)用的相機(jī)功能,下面小編把內(nèi)容整理分享到腳本之家平臺(tái),供大家參考
    2016-04-04
  • Android自定義View仿QQ健康界面

    Android自定義View仿QQ健康界面

    這篇文章主要為大家詳細(xì)介紹了Android自定義View仿QQ健康界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android實(shí)現(xiàn)果凍滑動(dòng)效果的控件

    Android實(shí)現(xiàn)果凍滑動(dòng)效果的控件

    這篇文章給大家主要介紹了利用Android如何實(shí)現(xiàn)果凍效果滑動(dòng)效果的控件,實(shí)現(xiàn)的效果類似于iOS有阻尼效果的滑動(dòng)控件,一般我們比較親切地稱之為果凍控件,常見的如微信里[我]的那個(gè)面板模塊,即使沒有再多的選項(xiàng),也不會(huì)很生硬的不允許用戶滑動(dòng)。下面來一起看看吧。
    2016-11-11
  • Android 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)

    Android 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)

    下面小編就為大家?guī)硪黄狝ndroid 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫

    Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊圖片上傳SQLite數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • android打開rar壓縮文件

    android打開rar壓縮文件

    這篇文章主要介紹了android打開rar壓縮文件示例,調(diào)用RAR for android 打開壓縮文件,需要的朋友可以參考下
    2014-03-03
  • Android 指紋功能實(shí)例代碼

    Android 指紋功能實(shí)例代碼

    本文通過一個(gè)demo給大家介紹了android指紋功能,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-12-12

最新評(píng)論