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>
そこに 2 つのこんにちはを表示します。1 つは垂直で、もう 1 つは水平です。理由を教えてください。どうも!この目標を達成するための新しい方法が歓迎されます。