作者AZHI (songs)
看板AndroidDev
标题[问题] 如何将继承view的class显示在layout上?
时间Tue Oct 8 23:48:56 2013
在继承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)
1F:推 zxc190:.addView...? 10/09 00:10
2F:→ cooper6334:要在xml加的话要打整个class的全名并且实作 10/09 02:30
3F:→ cooper6334:建构子(Context,AttributeSet) 10/09 02:30
4F:→ cooper6334:不然就是如1楼说的在程式里用AddView加 10/09 02:30
5F:→ cooper6334:还是你的问题是画不出来??? 10/09 02:33
我是在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)
6F:→ cooper6334:你有实作ViewClass(Context , AttributeSet)吗?log呢? 10/09 13:32
回c大,只有ViewClass(Context context)但加入ViewClass(Context , AttributeSet)
就可以了....可以请问是差在哪里吗?谢谢C大
※ 编辑: AZHI 来自: 123.205.97.25 (10/10 08:11)
8F:→ ljhgc:android:layout_width这些属性在建构时会透过AttributeSet传 10/11 01:34
9F:→ ljhgc:入class,所以你要实作ViewClass(Context , AttributeSet) 10/11 01:34
10F:→ ljhgc:如果你是在程式里用AddView加则不用实作这个建构子 10/11 01:35
谢谢c大和l大的回答
※ 编辑: AZHI 来自: 1.168.202.149 (10/11 22:14)