我想自己设计个view类,然后添加到xml布局里,最后显示在一个activity里,有人这样设计过吗,请赐教下
解决方案 »
- 为什么Android手机异地GPRS无法使用?该如何调试?
- 跪求大侠帮忙,createNormalSurfaceLocked() failed!!!
- 关于Socket连接问题
- 跪求,在android手机端,用代码来截图。高手快来哦
- 本科计算机专业,毕业后想往android开发方向发展,请问现在要自学什么课程呢?
- 兔子算法
- 有关EditText中设置inputType后minLines属性无效的问题
- 帮忙推荐一本Android界面设计的书
- 如何设置点击背景时可以将程序隐藏?
- 百度地图 MapView类 的 removeView接口为何不好使 急救大神解救!
- 要做一个自动化测试,请教如何测试Activity里面的方法?因为不能new一个实例对象
- apk 无法在真机上运行。
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
} //重载绘图方法
@Override
public void draw(Canvas canvas) {
}
}
2. xml 布局。将定义的View类像普通控件一样添加进来,控件名即完整的包名+类名<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:text="true" android:layout_height="wrap_content" android:id="@+id/btnTrue" android:layout_alignParentLeft="true"></Button> <android.HelloTest.MyView
android:id="@+id/panWhite" android:layout_below="@id/lblMessage"
android:layout_width="match_parent" android:layout_height="200px" />
</RelativeLayout>
3. Activity 中使用
跟普通控件一样,setContentView 之后findViewById 就行
this.setContentView(R.layout.touchtest);
MyView panWhite = (MyView)this.findViewById(R.id.panWhite);
参考:android自定义View的用法