垂直TextView滚动不起作用

时间:2019-05-09 12:05:49

标签: android

VerticalTextview滚动无法正常工作,以下是我的代码段:

public class VerticalTextView extends TextView {
    final boolean topDown;

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

        topDown = false;

    }

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

    @Override
    protected void onDraw(Canvas canvas){
        TextPaint textPaint = getPaint();
        textPaint.setColor(getCurrentTextColor());
        textPaint.drawableState = getDrawableState();

        canvas.save();

        if(topDown){
            canvas.translate(getWidth(), 0);
            canvas.rotate(90);
        }
        else
        {
            canvas.translate(0, getHeight());
            canvas.rotate(-90);
        }

        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());

        getLayout().draw(canvas);
        canvas.restore();
    }

即使我在下面的代码段中进行滚动 imagetextview

Textview image = new VerticalTextView(context,null);

     image.setEllipsize(TextUtils.TruncateAt.MARQUEE);
          image.setSingleLine(true);
          image.setMarqueeRepeatLimit(-1);
          image.setSelected(true);
          image.setFreezesText(true);
           image.setMovementMethod(new ScrollingMovementMethod());

2 个答案:

答案 0 :(得分:1)

我通过这种方式解决了您的问题:

<com.sample.VerticalTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view"
        android:scrollbars = "vertical"
        android:background="your_background"
        android:text="@string/TestLargeText" />

以及您的活动:

 TextView image = findViewById(R.id.text_view);
        image.setMovementMethod(new ScrollingMovementMethod());

如果您不想使用旋转框,则应在canvas.rotate(0)类中设置VerticalTextView

答案 1 :(得分:0)

您的意思是像在ScrollView中一样滚动吗?

您可以将较大的元素添加到诸如LinearLayout之类的布局中,并将该布局嵌入ScrollView中。

例如:

 <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <VerticalTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Imagine a very long text"/>

        </LinearLayout>

    </HorizontalScrollView>

(为了便于阅读,我已经缩短了很长的文字)

enter image description here

对不起,如果我误解了:)希望您能以某种方式解决它!无论如何,我认为您的TextView现在的外观以及您希望其外观/表现的图像或草图可以帮助您理解此问题。

相关问题