最近项目开发用到竖立的TextView。找了找资料大体都是旋转TextView 但是这样的话字也竖起来了看着很不爽。达不到古代诗词那样的效果。 重写了个Linearlayout。每个字是一个TextView。少量字数运行完美。多了可能会有性能问题。代码如下:
package com.coolsoft.attributionreject.tools; import android.content.Context; import android.util.AttributeSet; import android.widget.LinearLayout; import android.widget.TextView; public class VerticalTextView extends LinearLayout{ public VerticalTextView(Context context, AttributeSet attrs) { super(context, attrs); setOrientation(VERTICAL); this.context=context; } private String text; private Context context; private int color; private int size; public VerticalTextView(Context context) { super(context); setOrientation(VERTICAL); this.context=context; } public void setText(String text) { this.text=text; addText(); } private void addText() { removeAllViews(); if(text!=null) { char[] chara=text.toCharArray(); for(int i=0;i<chara.length;i++) { System.out.println("什么情况------"+text); TextView oneText=new TextView(context); oneText.setTextColor(color); oneText.setText(text.substring(i, i+1)); if(size>0) { oneText.setTextSize(size); } addView(oneText); } } } public void setTextColor(int color) { this.color=color; } public void setTextSize(int size) { this.size=size; } }
注意如果设置大小或者字体颜色的时候。一定要先设定。我没改懒得改了。要用的自己可以改下