Подтвердить что ты не робот

Вертикальный TextView, занимающий слишком много места в Android

Я пытаюсь создать что-то вроде следующего, используя LinearLayout и TableLayout:

enter image description here

Я застрял на вертикальном тексте, хотя. Он занимает больше места, чем нужно после вращения.

Ниже приведен XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/tv_CONSEQUENCES"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:rotation="-90"
        android:text="CONSEQUENCES"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

        </TableRow>
         <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

        </TableRow>
         <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

        </TableRow>
    </TableLayout>

</LinearLayout>

и вывод такой:

enter image description here

Вертикальный текст занимает больше места, чем нужно, и не весь текст отображается. :(

4b9b3361

Ответ 2

Вы можете использовать код ниже:

android:rotation="270"

Ответ 3

Ни одно из решений не помогло мне, но я нашел этот пост Марка Эллисона: 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();
    }
}

Вращение осуществляется гравитацией. Поэтому не забудьте установить это в своем 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" />

Ответ 4

вам нужно расширить TextView и переопределить его onDraw

Ответ 5

        android:rotation="-90"
        android:layout_gravity="center"

Работал для меня.

Ответ 6

в XML файл просто положить

<TextView> 
 android:layout_width="wrap content"
 android:layout_height="wrap content"
 android:rotation="270"
</TextView>

Ответ 7

    val textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    textPaint.textSize=toPx(16)
    textPaint.color=Color.parseColor("#FFFFFF")
    canvas.save()
    canvas.translate(50F, 60F)
    canvas.rotate(90F)
    canvas.drawPaint(textPaint)
    canvas.drawText("YourText", 0F,0F, textPaint)
    canvas.restore()

Ответ 8

почему бы не поместить прописку? так что она пойдет в центре экрана. Попробуйте, если вам нравится

Ответ 9

Поместите весь текст в Линейный макет и попытайтесь дать правильный вес макета, чтобы решить вашу проблему.

Ответ 10

Вот хак для создания вертикального текстового представления. Таким образом, сценарий был мне нужно показать статический тег в правой части экрана. поэтому я просто использовал "\n" для новой строки, так как мой текст был меньше.

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

Вы можете проверить изображение, которое я прикрепил. хотя это не рекомендуется, если вы показываете данные из динамически. enter image description here

<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" />

Ответ 11

Если вы хотите воспроизвести свой образ, я бы использовал GridView или TableLayout без кнопок, просто установите правильных слушателей, если вам нужно предпринять какие-либо действия. Таким образом вы можете установить фиксированные высоты и ширину.