Android 向布局中添加垂直按钮

Android 向布局中添加垂直按钮,android,android-layout,Android,Android Layout,我已经创建了一个布局并试图添加一个垂直按钮,但我似乎无法将其从水平更改为垂直。垂直按钮必须位于图像视图的旁边 布局 使用线性布局代替按钮。在线性布局中,查看文本视图。使用android:rotation=“90”将文本视图旋转90度。这与按钮的工作原理相同。使用文本视图而不是按钮。按钮也是从TextView类扩展而来。下面是自定义TextView,它将使TextView垂直 希望这能解决问题 public class VerticalTextView extends TextView {

我已经创建了一个
布局
并试图添加一个
垂直按钮
,但我似乎无法将其从
水平
更改为
垂直
。垂直按钮必须位于图像视图的旁边

布局


使用线性布局代替按钮。在线性布局中,查看文本视图。使用android:rotation=“90”将文本视图旋转90度。这与按钮的工作原理相同。

使用文本视图而不是按钮。按钮也是从TextView类扩展而来。下面是自定义TextView,它将使TextView垂直

希望这能解决问题

 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中使用


在res/values/styles.xml中创建样式


20dp
20便士

在这里您可以找到您的答案。这可能会有帮助。谢谢。但是有没有一种方法可以使文本垂直?只需在xml中使用android:rotation=“90”即可。
 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();
    }
}
<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" />
<?xml version="1.0" encoding="utf-8"?>
<resources
    xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="verticalTextStyle"
        parent="android:Widget.TextView">
        <item name="android:padding">20dp</item>
        <item name="android:textSize">20sp</item>
    </style>
</resources>