1

我想扩展一个 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>

有显示两个你好!一个是垂直的,另一个是水平的。有人能告诉我为什么吗?谢谢!一些实现这一目标的新方法受到欢迎。

4

2 回答 2

3

检查此实现。它使用路径而不是循环来绘制文本。

public class VerticalTextView extends TextView {

    public VerticalTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VerticalTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalTextView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        final ColorStateList csl = getTextColors();
        final int color = csl.getDefaultColor();
        final int paddingBottom = getPaddingBottom();
        final int paddingTop = getPaddingTop();
        final int viewWidth = getWidth();
        final int viewHeight = getHeight();
        final TextPaint paint = getPaint();
        paint.setColor(color);
        final float bottom = viewWidth * 9.0f / 11.0f;
        Path p = new Path();
        p.moveTo(bottom, viewHeight - paddingBottom - paddingTop);
        p.lineTo(bottom, paddingTop);
        canvas.drawTextOnPath(getText().toString(), p, 0, 0, paint);
    }
}
于 2012-11-18T17:03:09.143 回答
0

尝试一下

package artefatoscs.util;
public class VerticalTextView extends TextView{

    public VerticalTextView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected boolean setFrame(int l, int t, int r, int b){
        return super.setFrame(l, t, l+(b-t), t+(r-l));
    }

    @Override
    public void draw(Canvas canvas){

        canvas.translate(0, getWidth());
        canvas.rotate(-90);

        canvas.clipRect(0, 0, getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
        super.draw(canvas);
    }
}

res\values\attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="VerticalLabelView">
        <attr name="text" format="string" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

你的.xml

<artefatoscs.util.VerticalTextView
            android:id="@+id/txtDM"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:background="@color/white"
            android:padding="2sp" />
于 2016-01-12T15:17:29.823 回答