然后将我们自定义的View 加入到main.xml 布局文件中, 在这里设置View的背景色为白色,是为了更好地展现其中的内容。代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- <Button
- android:layout_width="match_parent"
- android:layout_height="40dip"
- android:text="下一张图" />
- <com.viewTest.MyView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#FFFFFF"/>
- </LinearLayout>
初始的activity.java文件并不需要修改。最后的效果如下图所示:

