2014-02-17 101 views
0

我已經擴展FrameLayout類用於在片段轉換中製作幻燈片滑出動畫。我想以編程方式獲得xFraction值。Android。擴展FrameLayout。找不到屬性的setter/getter xFraction異常

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="70dp" 
     android:layout_height="70dp" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="15dp" 
     android:src="@+drawable/logo" /> 

    <com.example.helpers.CustomFrameLayout 
     android:id="@+id/fragment" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

CustomFrameLayout看起來像

public class CustomFrameLayout extends FrameLayout { 


    public CustomFrameLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    public CustomFrameLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    public CustomFrameLayout(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public float getXFraction() { 
     return getX()/getWidth(); 
    } 

    public void setXFraction(float xFraction) { 
     final int width = getWidth(); 
     setX((width > 0) ? (xFraction * width) : -9999); 
    } 
} 

當動畫按鈕,點擊後開始拋出一個異常:

Can't find native method using JNI, use reflectionjava.lang.NoSuchMethodError: no method with name='setXFraction' signature='(F)V' in class Landroid/widget/RelativeLayout; 
02-17 13:57:33.369: E/PropertyValuesHolder(12223): Couldn't find setter/getter for property xFraction with value type float 

Animate the transition between fragments - 我有我的想法來自那裏。但它不起作用。請幫助我

+0

http://stackoverflow.com/questions/19372436/doing-a-push-fragment-animation?answertab=active#tab-top - 我在這裏找到了答案。 – user2918962

回答

0

看起來您正嘗試在父代RelativeLayout上調用method:setXFraction(),而不是在您的com.example.helpers.CustomFrameLayout上。

你應該參考您的CustomFrameLayout像這樣(假設你已經有了參考命名parentRelativeLayout父佈局):

CustomFrameLayout customFrameLayout = (CustomFrameLayout) parentRelativeLayout.findViewById(R.id.fragment); 

現在你可以調用適當的參考如setXFraction()

customFrameLayout.setXFraction(25.4); 
+0

公共無效的onClick(視圖v){ \t \t \t \t \t \t parentAct.setTransaction(getFragmentManager() \t \t \t \t \t \t \t \t .beginTransaction()); 。 \t \t \t \t \t \t parentAct.getTransaction()setCustomAnimations( \t \t \t \t \t \t \t \t R.anim.slide_from_right_to_screen, \t \t \t \t \t \t \t \t R.anim.slide_from_screen_to_left); \t \t \t \t \t \t parentAct.getTransaction()。替換(R.id。片段, \t \t \t \t \t \t \t \t parentAct.getNativeLoginFr()); parent(); parent(); parent(); parent.getTransaction()。commit(); \t \t \t \t \t} - 所以我不調用這個方法,它應該工作本身 – user2918962

+0

我slide_from_right_to_screen.xml含量 user2918962

0

無論您試圖製作動畫的視圖都應該有setXFraction方法。

如果你膨脹這整個xml,它會檢查你的相對佈局中的setXFraction。

我猜getNativeLoginFr是給你的觀點,它是充氣整個XML ..

您只需要該膨脹爲的FrameLayout動畫工作。