如何创建自定义ProgressBar并在Widget中使用它?
我想在 Widget 中使用我自己的扩展 ProgressBar 类。我创建了非常简单的 MyProgressBar,如果我将其放入标准活动布局中,它就可以工作。当然,我还在设计器的 MyProgressBar 属性中将 Style 属性设置为“?android:attr/progressBarStyleHorizontal”。
但是,如果我做同样的事情并将其放置到 Widget 布局中,它在 Eclipse 的布局设计器中可见良好,但是,Widget 在 traget 中不起作用。仅显示“问题加载小部件”消息,而不是小部件布局...在目标设备上...
MyProgressBar 的源代码在这里:
public class MyProgressBar extends ProgressBar {
public MyProgressBar(Context context) {
super(context);
}
public MyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// First draw the regular progress bar, then custom draw our text
super.onDraw(canvas);
}
}
I would like use my own extended ProgressBar class in Widget. I has created really simple MyProgressBar and if I place this into standart activity layout, it works. Of course I set also Style attribiute to "?android:attr/progressBarStyleHorizontal" in MyProgressBar property in designer.
But if I do same and place it to Widget layout, its visible good in layout designer in eclipse, however, widget does not work in traget. There is only "Problem loaing widget" message showed instead of widget layout .... on target device....
Source code for MyProgressBar is here :
public class MyProgressBar extends ProgressBar {
public MyProgressBar(Context context) {
super(context);
}
public MyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
// First draw the regular progress bar, then custom draw our text
super.onDraw(canvas);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
小部件不支持自定义视图。
如果你只是改变进度条的背景,你可以使用系统的进度条,并改变布局xml中的drawable。
Widget don't support custom view.
If you just change the progressbar's background,you can use the system's progressbar,and change the drawable in the layout xml.
App Widget可以支持以下布局类:FrameLayout、LinearLayout、RelativeLayout、GridLayout。
以及以下小部件类:AnalogClock、Button、Chronometer、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView、AdapterViewFlipper。
不支持这些类的后代。
复制自: http://developer.android.com/guide/topics/ appwidgets/index.html#CreatingLayout
App Widget can support the following layout classes: FrameLayout, LinearLayout, RelativeLayout, GridLayout.
And the following widget classes: AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView, ViewFlipper, ListView, GridView, StackView, AdapterViewFlipper.
Descendants of these classes are not supported.
Copied from: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout