2012-11-18 19 views
1

我想擴展一個VerticalTextView類,它可以使內容顯示垂直文本。 例如: android:text="Hello" 它表明這樣的:我自己的自定義textview可以垂直顯示文本,但有些問題!誰可以幫我

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> 

有顯示兩個你好!一個是垂直的,另一個是水平的。有人告訴我爲什麼?謝謝!實現這一目標的一些新方法受到歡迎。

+0

這是什麼? 'getLayout()。draw(canvas);'你在這裏做什麼? – Simon

+0

如果刪除了getLayout()。draw(canvas),它將不會顯示。您可以嘗試〜 – daimajia

+0

然後您正在做其他事情。請顯示您的onCreate()並編輯您的XML,以便*完全*您正在使用而沒有評論。您不需要將畫布繪製到控件上,這就是onDraw的用途,您正在重寫超類。 – Simon

回答

3

檢查this implementation出。它使用Path來繪製文本而不是循環。

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); 
    } 
} 
+0

我做了一個測試,我添加了getLayout()。draw(canvas);在canvas.drawTextOnPath(getText()。toString(),p,0,0,paint)之後;兩個'你好'出現(一個垂直的一個水平),爲什麼會發生這種情況。 – daimajia

+0

如果沒有您當前的代碼,很難說清楚。你確定你的佈局/代碼只有一個VerticalTextView嗎? –

+0

是的,我確定。你可以嘗試一下,添加getLayout()。draw(canvas);在canvas.drawTextOnPath之後。 – daimajia

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); 
    } 
} 

資源\值\ 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> 

your.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" /> 
相關問題