Vertical TextView taking too much space in Android

后端 未结 11 1664
鱼传尺愫
鱼传尺愫 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:

    <com.stylingandroid.verticaltext.VerticalTextView
        style="@style/verticalTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom|right"
        android:text="@string/text" />
    
    0 讨论(0)
  • If you want to reproduce your image I would use a GridView or a TableLayout with no buttons, just set the proper listeners if you need to do any action. This way you can set fixed heights and widths.

    0 讨论(0)
  • 2021-02-01 04:31

    Put all the Text view in Linear Layout and try to give proper Layout weight to solve your problem

    0 讨论(0)
  • 2021-02-01 04:31

    Here is a hack to create vertical text view. So the scenario was I need to show a static tag on the right side of the screen. so I just used "\n" for the new line as my text was less.

    android:text="A\nC\nT\nI\nO\nN"

    You can check the image I've attached. although it's not suggested if you showing data from Dynamically.

    <TextView
        android:id="@+id/tv_action"
        android:layout_width="20dp"
        android:layout_height="126dp"
        android:background="@drawable/bg_red_left_rcorner"
        android:gravity="center"
        android:padding="2dp"
        android:textStyle="bold"
        android:text="A\nC\nT\nI\nO\nN"
        android:textColor="@color/white"
        app:layout_anchor="@+id/drawer_layout"
        app:layout_anchorGravity="end|center" />
    
    0 讨论(0)
  • 2021-02-01 04:40

    in .xml file just put

    <TextView> 
     android:layout_width="wrap content"
     android:layout_height="wrap content"
     android:rotation="270"
    </TextView>
    
    0 讨论(0)
提交回复
热议问题