android 自定义textView,实现排版对齐和换行
来源:互联网 发布:java多线程文件上传 编辑:程序博客网 时间:2024/05/14 17:35
转自:http://blog.csdn.net/dyc333236081818/article/details/7467835
android开发中的textview可以自动换行,但是对于显示纯英文文字来说很好用,如果夹杂了中文字符后,全角字符和半角字符混在一块儿,就会出现文字排版参差不齐,超级难看,这就需要重写textview来实现我们需要的显示方式。
下面贴上我的代码:
main.xml
- <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:ae="http://www.angellecho.com/"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@android:color/black"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <ScrollView
- android:layout_width="wrap_content"
- android:layout_height="200dp" >
- <com.wigit.MyTextView2
- android:id="@+id/view"
- android:layout_width="fill_parent"
- android:layout_height="200dp"
- android:layout_marginTop="20dp"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:background="@android:color/darker_gray"
- ae:marginLeft="20"
- ae:marginRight="20"
- ae:paddingLeft="20"
- ae:paddingRight="20"
- ae:textColor="#FFFFFF"
- ae:textSize="30" />
- </ScrollView>
- </LinearLayout></span>
- package com.wigit;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class MyTextView2 extends TextView{
- private final String namespace = "http://www.angellecho.com/";
- private String text;
- private float textSize;
- private float paddingLeft;
- private float paddingRight;
- private float marginLeft;
- private float marginRight;
- private int textColor;
- private Paint paint1 = new Paint();
- private float textShowWidth;
- public MyTextView2(Context context, AttributeSet attrs) {
- super(context, attrs);
- text = attrs.getAttributeValue(
- "http://schemas.android.com/apk/res/android", "text");
- textSize = attrs.getAttributeIntValue(namespace, "textSize", 15);
- textColor = attrs.getAttributeIntValue(namespace, "textColor",Color.WHITE);
- 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);
- textShowWidth = ((Activity) context).getWindowManager().getDefaultDisplay().getWidth() - paddingLeft - paddingRight - marginLeft - marginRight;
- }
- @Override
- protected void onDraw(Canvas canvas) {
- //super.onDraw(canvas);
- 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;
- }
- canvas.drawText(textCharArray, i, 1, paddingLeft + drawedWidth,
- (lineCount + 1) * textSize, paint1);
- drawedWidth += charWidth;
- }
- setHeight((lineCount + 1) * (int) textSize + 5);
- }
- }
测试activity
- package com.text;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import com.wigit.MyTextView2;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.AssetManager;
- import android.os.Bundle;
- import android.text.method.ScrollingMovementMethod;
- public class MTextViewActivity extends Activity {
- MyTextView2 view;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main1);
- view = (MyTextView2) findViewById(R.id.view);
- view.setText(getAssetsString(this,"1.txt"));
- view.setMovementMethod(ScrollingMovementMethod.getInstance());
- }
- <span style="white-space:pre"> </span>//读取assets文件夹下文本字符串
- public String getAssetsString(Context context,String fileName){
- StringBuffer sb = new StringBuffer();
- try {
- AssetManager am = context.getAssets();
- InputStream in = am.open(fileName);
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String line;
- while((line = reader.readLine())!=null){
- line += ("\n");
- sb.append(line);
- }
- reader.close();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return sb.toString();
- }
- }
并附上例子如下(免积分哦):
http://download.csdn.net/detail/dyc333236081818/4231656
0 0
- android 自定义textView,实现排版对齐和换行
- android 自定义textView,实现排版对齐和换行
- android 自定义textView,实现排版对齐和换行
- TextView 换行对齐自定义
- Android 自定义TextView 实现文字对齐排版且支持点击划词
- 实现TextView 文字排版,分散两端对齐
- textview 文字排版问题,实现两端对齐
- Android自定义TextView排版优化
- Android TextView 解决文字换行排版
- android textview 自动换行 整齐排版
- android textview 自动换行 整齐排版
- android textview 自动换行 整齐排版
- 【TextView】自定义TextView中文换行排版问题修复
- Android textview文字对齐换行问题
- 关于自定义TextView排版分散对齐的思路
- Android 自定义TextView 自动换行
- Android TextView自动换行文字,排版混乱的原因和解决方法
- Android TextView自动换行文字,排版混乱的原因和解决方法
- HDU 吃糖果
- WebLogic部署Web项目生成路径
- 半结构数据
- IOS 使用CoreText实现表情文本URL等混合显示控件
- 十二周项目一
- android 自定义textView,实现排版对齐和换行
- 欢迎使用CSDN-markdown编辑器
- Quartz 2D 涂鸦
- 同步异步与阻塞非阻塞
- ACM-把字母的电话号码转化成数字的电话号码
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET路由(十)
- 第十二周上机项目2摩托车继承自行车和机动车
- 第12周项目 程序阅读(5.a)
- 第十二周项目1——教师兼干部类