2013-02-15 40 views
0

以下設置不起作用,有人知道爲什麼(我似乎無法找到片段中自定義曲面視圖的示例)。如何顯示包含自定義SurfaceView的片段

我surfaceView類是目前只是空:

class MySurfaceView extends SurfaceView implements Runnable{ 

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

@Override 
public void run() { 
    // TODO Auto-generated method stub 

} 



} 

我對片段XML看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout2" 
    android:layout_width="500dp" 
    android:layout_height="600dp" 
    android:layout_gravity="fill" 
    android:background="#000000" > 


    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" /> 


    <com.example.android.fragments.MySurfaceView 
     android:id="@+id/surfaceview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 


    </LinearLayout> 

然後在實際片段本身的OnCreateView我打電話:

return inflater.inflate(R.layout.article_view, container, false); 

任何想法,爲什麼這不會在一起?

這事做與XML,因爲當我做

<SurfaceView 
    android:id="@+id/surfaceview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

然後正常工作。

回答

0

答案很簡單,如果你檢查:

http://developer.android.com/guide/components/fragments.html#Creating

,你會看到,onCreateView你應該返回視圖將被用來展示片段

@Override 
    public View onCreateView(String name , Context context , AttributeSet attrs) 
    { 
     // TODO Auto-generated method stub 
     return super.onCreateView(name , context , attrs); 
    } 

你應該返回查看所以基本上你應該回到那裏你的

MySurfaceView 

//編輯

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

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

    public MySurfaceView (Context context) 
    { 
     super(context); 
    } 

嘗試實施這一觀點的所有3個構造

+0

我認爲你的答案是不正確的,因爲「要爲你的片段繪製一個UI,你必須從這個方法返回一個視圖,這是你的片段佈局的根。 SurfaceView不是根視圖,它只是XML文檔中的一個組件,因爲我也有一個按鈕。所以這就是我正在做的「返回inflater.inflate(R.layout.article_view,容器,false);」我膨脹並返回包含SurfaceView的根視圖。 – drlobo 2013-02-15 15:45:15

1

如果你想要的是顯示您的自定義SurfaceView(「MySurfaceView」)的片段,然後只需做到以下幾點:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return new MySurfaceView(getActivity()); 
    }