[問題] 如何將繼承view的class顯示在layout上?

看板AndroidDev作者 (songs)時間12年前 (2013/10/08 23:48), 編輯推噓1(109)
留言10則, 3人參與, 最新討論串1/1
在繼承view的class中 使用onDraw()繪圖 程式碼: public class MyView extends View { ............. ............. protected void onDraw(Canvas canvas) { ........... ........... } } 然後,在main.xml中新增一個FrameLayout 請問板上大大 如何將class中所畫的東西顯示在FrameLayout中呢? 備註:class是有觸控的繪圖功能 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.205.97.25 ※ 編輯: AZHI 來自: 123.205.97.25 (10/09 00:02)

10/09 00:10, , 1F
.addView...?
10/09 00:10, 1F

10/09 02:30, , 2F
要在xml加的話要打整個class的全名並且實作
10/09 02:30, 2F

10/09 02:30, , 3F
建構子(Context,AttributeSet)
10/09 02:30, 3F

10/09 02:30, , 4F
不然就是如1樓說的在程式裡用AddView加
10/09 02:30, 4F

10/09 02:33, , 5F
還是你的問題是畫不出來???
10/09 02:33, 5F
我是在xml的layout中加入view <view class="com.example.canvasdraw.ViewClass"←這邊是我的CLASS android:layout_width="fill_parent" android:layout_height="fill_parent" /> 但無法顯示出來,執行後會跳出 不知道原因為何... ※ 編輯: AZHI 來自: 123.205.97.25 (10/09 12:47)

10/09 13:32, , 6F
你有實作ViewClass(Context , AttributeSet)嗎?log呢?
10/09 13:32, 6F
回c大,只有ViewClass(Context context)但加入ViewClass(Context , AttributeSet) 就可以了....可以請問是差在哪裡嗎?謝謝C大 ※ 編輯: AZHI 來自: 123.205.97.25 (10/10 08:11)

10/10 19:45, , 7F
10/10 19:45, 7F

10/11 01:34, , 8F
android:layout_width這些屬性在建構時會透過AttributeSet傳
10/11 01:34, 8F

10/11 01:34, , 9F
入class,所以你要實作ViewClass(Context , AttributeSet)
10/11 01:34, 9F

10/11 01:35, , 10F
如果你是在程式裡用AddView加則不用實作這個建構子
10/11 01:35, 10F
謝謝c大和l大的回答 ※ 編輯: AZHI 來自: 1.168.202.149 (10/11 22:14)
文章代碼(AID): #1IL2bgLA (AndroidDev)