13

我想用XML在Android應用程序中繪製對角線,但它不起作用。它只是繪製一條水平線。Android XML如何旋轉線條?

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".TestActivity" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     style="@style/diagonalStyle"> 
    </RelativeLayout> 

</RelativeLayout> 

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 

    <style name="diagonalStyle"> 
     <item name="android:background">@drawable/background</item> 
    </style> 

</resources> 

background.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item> 
     <rotate 
      android:fromDegrees="0" 
      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> 

</layer-list> 

回答

31

你真的Ø只需要一個號碼變化就可以使其工作。只是改變fromDegrees 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> 

的旋轉抽拉http://developer.android.com/reference/android/graphics/drawable/RotateDrawable.html

實際使用物業的動畫格式http://developer.android.com/guide/topics/resources/animation-resource.html

而你是一個非動畫對角線,你想讓它開始了在45度,並最終在45度也。因此將它們都設置爲45是常態。

+0

謝謝你,我還以爲fromDegrees意味着在點0和toDegrees是爲「n」點。我不知道它是指動畫。 – 2013-03-06 05:28:31

+0

這是非直觀的。我很樂意提供幫助。 – HalR 2013-03-06 06:07:30

+1

你好,我想要的一樣,它會重複像'android:tileMode =「repeat」'這可能嗎? – 2013-04-30 07:34:01

0

你可以試試這個: 創建佈局 「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"/> 

創建繪製形狀 「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>