创建完自定义的控件后,你可以像使用其他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);

}

相关文章:

  • 2021-04-09
  • 2022-12-23
  • 2021-07-15
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2021-12-15
相关资源
相似解决方案