2015-10-14 48 views
0

這裏添加的視圖的子類是主要佈局文件的一部分:在主要佈局

<FrameLayout 

    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity = "center" > 
    <view 
     android:id="@+id/board" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     class="com.drawshapes.DrawCircles"></view> 
</FrameLayout> 

它給了我一個編譯錯誤。我不確定我是否正確接近它。我應該看碎片嗎?或者可能添加另一個活動文件?或者一個佈局文件? 還有來自谷歌一個片段,像這樣的東西開始:

 <com.drawshapes.DrawCircles> 
     android. ..... 
    </> 

但我不知道往哪裏放他們,如果他們進入一個資源文件或創建另一個佈局文件。

謝謝你的幫助。

+0

粘貼您的DrawCircle類和錯誤日誌 – droidev

回答

0

試試這個

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" > 

    <com.drawshapes.DrawCircles 
     android:id="@+id/board" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" /> 

</FrameLayout> 
+0

該標記仍然不起作用。錯誤指向setContentView(R.layout.main_activity);在主要活動課上。 – theMobDog

+0

你把'DrawCircles'類放在哪裏?它是庫還是模塊? – myoungjin

+0

我發現我做錯了。事實證明,爲了將視圖的子類添加爲XML中的自定義視圖,我們必須實現此構造函數: public MyView(Context context,AttributeSet attrs) – theMobDog

0

找到了答案:

在實施的視圖子類來佈局XML文件中使用,使用這種格式:

<FrameLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center" > 

<com.drawshapes.DrawCircles 
    android:id="@+id/board" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" /> 

</FrameLayout> 

(感謝dotkebi。 )

另外我必須實現這個構造函數:

// gets called when adding this sub-view class from XML 
    public DrawShapes(Context con, AttributeSet attrs) { 
    super(con, attrs); 
    Log.v("constructor", "context & attributes "); 
    myList.add(new Shape(120, 170, 21,"square")); 
}