13

Próbuję narysować ukośną linię w aplikacji na Androida z plikiem XML, ale nie działa. Po prostu rysuje poziomą linię.Jak obrócić wiersz w systemie 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> 

Odpowiedz

31

Naprawdę o potrzebował jednej zmiany liczby, aby to zadziałało. Wystarczy zmienić fromDegrees do 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> 

Obróć odkształcalne http://developer.android.com/reference/android/graphics/drawable/RotateDrawable.html

rzeczywiście wykorzystuje format animacji Property http://developer.android.com/guide/topics/resources/animation-resource.html

Zważywszy robicie nie animowane ukośną linię, chcesz go rozpocząć się pod kątem 45 stopni, a kończy na 45 stopni. Ustawienie ich na 45 jest normą.

+0

dziękuję, myślałem, że fromDegrees przeznaczona na punkt 0 i toDegrees był dla punktu "n". Nie wiedziałem, że to dotyczy animacji. –

+0

Jest nieintuicyjny. Cieszę się że mogę pomóc. – HalR

+1

Witam Chciałem to samo i że będzie się powtarzać jak "android: tileMode =" repeat "' to jest możliwe? –

0

Można spróbować to: utworzyć układ "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"/> 

Tworzenie rozciągliwej kształt "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> 
Powiązane problemy