How get Vertical Button view from xml

倖福魔咒の 提交于 2019-12-10 11:43:58

问题


I want create a Button in Vertically.May be we can by extending a button and re-render(rotate) the canvas to vertical we can get the custom Button. But i need it from xml.check the graphical representation.i need a button like this.


回答1:


Please, see link below, there should be solution for your problem

http://blog.stylingandroid.com/archives/796

This is tutorial about how to create vertical-textView But because of Button class extends TextView this tutorial should work for Buttons too

UPDATE:

1) Create a style in res/values/styles.xml which we’l use later:

    <?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>

2) replace the default string named hello with one named text to res/values/strings.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="text">Vertical Text</string>
        <string name="app_name">VerticalText</string>
    </resources>

3) Create custom VerticalTextView class

    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();
        }
    }

4) change our main layout in res/layout/main.xml to include a VerticalTextView control:

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

5) Result

Source: http://blog.stylingandroid.com/archives/796



来源:https://stackoverflow.com/questions/23127237/how-get-vertical-button-view-from-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!