How rotate line in Android XML?

前端 未结 2 579
囚心锁ツ
囚心锁ツ 2020-12-15 06:01

I\'m trying to draw a diagonal line in an Android app with the XML, but it is not working. It simply draws a horizontal line.

main.xml:



        
相关标签:
2条回答
  • 2020-12-15 06:30

    You can try this : Create a layout "divider.xml"

    <?xml version="1.0" encoding="utf-8"?>
    <View android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="@dimen/one_dp"
        android:layout_weight=".1"
        android:background="@drawable/transparent_divider"
        android:padding="5dp"
        android:rotation="180"/>
    

    Create a drawable shape "transparent_divider.xml":

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <size android:height="1dp" />
        <solid android:color="#808080" />
    </shape>
    
    0 讨论(0)
  • 2020-12-15 06:40

    You really only needed one number change to get it to work. Just Change the fromDegrees to 45:

    <item>
        <rotate
                android:fromDegrees="45"
                android:toDegrees="45"
                android:pivotX="50%"
                android:pivotY="50%" >
            <shape
                    android:shape="line"
                    android:top="1dip" >
                <stroke
                        android:width="1dip"
                        android:color="#FF0000" />
            </shape>
        </rotate>
    </item>
    

    The rotate drawable http://developer.android.com/reference/android/graphics/drawable/RotateDrawable.html

    actually uses the Property Animation format http://developer.android.com/guide/topics/resources/animation-resource.html

    Whereas you are making a non-animating diagonal line, you want it to start out at 45 degrees, and end up at 45 degrees also. So setting them both to 45 is the norm.

    0 讨论(0)
提交回复
热议问题