Vertical TextView taking too much space in Android

后端 未结 11 1692
鱼传尺愫
鱼传尺愫 2021-02-01 04:02

I am trying to create something like the following using LinearLayout and TableLayout:

\"enter

11条回答
  •  心在旅途
    2021-02-01 04:27

    None of the solutions worked for me but I found this blog post by Mark Allison: https://blog.stylingandroid.com/verticaltext-part-1/

    public class VerticalTextView extends TextView
    {
        final boolean topDown;
    
        public VerticalTextView( Context context, 
            AttributeSet attrs )
        {
            super( context, attrs );
            final int gravity = getGravity();
            if ( Gravity.isVertical( gravity )
                && ( gravity & Gravity.VERTICAL_GRAVITY_MASK ) 
                == Gravity.BOTTOM )
            {
                setGravity( 
                    ( gravity & Gravity.HORIZONTAL_GRAVITY_MASK )
                        | Gravity.TOP );
                topDown = false;
            }
            else
            {
                topDown = true;
            }
        }
    
        @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();
        }
    }
    

    The rotation is done by the gravity. So be sure to set this in your xml:

    
    

提交回复
热议问题