2014-10-02 209 views
0

我希望我的視圖從佈局的右下角移動到中心。我在RelativeLayout中只有一個textView,並寫了一個簡單的代碼來測試它,但沒有奏效。翻譯動畫無效

這是我的動畫XML;

<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fromXDelta="0%" 
    android:fromYDelta="0%" 
    android:toXDelta="50%p" 
    android:toYDelta="50%p" 
    android:duration="1000" 
    android:fillAfter="true" /> 

這是我的佈局文件;

<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:baselineAligned="false" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="x" /> 

</RelativeLayout> 

這是活動的相關部分;

x = (TextView) findViewById(R.id.textView1); 
    Animation translate = AnimationUtils.loadAnimation(MainActivity.this, 
      R.anim.viewanimation); 
    x.startAnimation(translate); 

問題是,textView「x」不移動。我讀過在XML文件中指定%p使其相對於父項。由於我希望我的視圖從原始位置移動到屏幕中心,因此我將「從」和「到」值設置爲0%50%p。我觀察到,當我刪除%p屬性並使其從諸如0%之類的東西移動到-100%時,它會正確移動。所以相對定位在這裏似乎不起作用。我可能會做錯什麼?

回答

0

問題解決了。相對值應爲負值,如下所示;

android:toXDelta="-50%p" 
android:toYDelta="-50%p" 

由於正值會將視圖移出佈局。所以,%0p值對應於佈局的右下角。