使用自定义的控件

mac2022-06-30  85

创建完自定义的控件后,你可以像使用其他Android控件一样,在代码或layout中使用它们。下面的代码片段显示了在重写的onCreate方法中,如何创建CompassView并设定给Activity

 

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

CompassView cv = new CompassView(this);

setContentView(cv);

cv.setBearing(45);

}

 

想在layout资源中使用相同的控件,需要在layout定义新节点时指定全名。如下的XML片段所示:

 

<com.paad.compass.CompassView

android:id=”@+id/compassView”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

/>

 

你可以膨胀layout并得到CompassView的引用,如下代码所示:

 

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

CompassView cv = (CompassView)this.findViewById(R.id.compassView);

cv.setBearing(45);

}

转载于:https://www.cnblogs.com/xirihanlin/archive/2009/07/24/1530255.html

相关资源:C#自定义控件库
最新回复(0)