我想扩展一个 VerticalTextView 类,它可以使内容显示垂直文本。示例:
android:text="Hello"
它显示如下:
H
e
l
l
o
我尝试覆盖 onDraw 函数。下面的代码:
public class VerText extends TextView {
public VerText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VerText(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
TextPaint textPaint = getPaint();
textPaint.drawableState = getDrawableState();
textPaint.setColor(Color.BLACK);
String textString = (String) getText();
//textString = "Hello";
for (int i = 0; i < textString.length(); i++) {
canvas.drawText(textString.charAt(i) + "", getMeasuredWidth() / 2
- getTextSize() / 2, (i + 1) * getTextSize(), textPaint);
}
getLayout().draw(canvas);
}
} 这很简单,对吧?我在这里遇到问题,当我使用时
<RelativeLayout 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"
tools:context=".TestActivity" >
<com.example.testmycustom.VerText
android:id="@+id/verText1"
android:layout_width="60dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="119dp" />
</RelativeLayout>
并且//textString = "Hello";
有用,效果很好,但是当我使用
<RelativeLayout 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"
tools:context=".TestActivity" >
<com.example.testmycustom.VerText
android:id="@+id/verText1"
android:layout_width="60dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Hello"//!!!!only added this one!!!! and canceled comment textString="Hello"
android:layout_marginLeft="119dp" />
</RelativeLayout>
有显示两个你好!一个是垂直的,另一个是水平的。有人能告诉我为什么吗?谢谢!一些实现这一目标的新方法受到欢迎。