基于现有小部件的Android自定义小部件
我希望我的问题可以理解,因为我是一个Android初学者,英语不是我的母语 我想创建一个自定义小部件,其中包括两个TextView、ProgressBar和Button。目标是能够在XML布局文件中声明我的自定义小部件,并使用自定义属性定义按钮文本等。。。但是内部Android小部件的配置将是相同的,并在我的类中定义 我已经找到了如何声明自定义属性和创建自定义小部件的方法,但在现有Android小部件的简单放置案例中,我没有找到任何文档或示例。我很惊讶,也许我找错了方向 下面是我正在测试的简单代码 自定义类:基于现有小部件的Android自定义小部件,android,widget,ondraw,Android,Widget,Ondraw,我希望我的问题可以理解,因为我是一个Android初学者,英语不是我的母语 我想创建一个自定义小部件,其中包括两个TextView、ProgressBar和Button。目标是能够在XML布局文件中声明我的自定义小部件,并使用自定义属性定义按钮文本等。。。但是内部Android小部件的配置将是相同的,并在我的类中定义 我已经找到了如何声明自定义属性和创建自定义小部件的方法,但在现有Android小部件的简单放置案例中,我没有找到任何文档或示例。我很惊讶,也许我找错了方向 下面是我正在测试的简单代
public class CastleViewItem extends View {
private TextView item;
public CastleViewItem (Context c, AttributeSet attributes) {
super(c, attributes);
item = new TextView(c);
TypedArray attrs = c.obtainStyledAttributes(attributes, R.styleable.CastleViewItem);
item.setText(attrs.getString(R.styleable.CastleViewItem_name).toString());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// I suppose there is some code to add here.
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(200, 200);
// item.getWidth()/item.getHeight() return 0, so I fixed temporarily the value to ensure
// the widget will be displayed.
}
}
XML自定义属性声明:
<resources>
<declare-styleable name="CastleViewItem">
<attr name="name" format="string" />
</declare-styleable>
</resources>
XML布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ffbdx="http://schemas.android.com/apk/res/bdx.fleurey"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<bdx.fleurey.CastleViewItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ffbdx:name="Moat" />
</LinearLayout>
此代码不显示任何内容。
我希望我说得很清楚,提前谢谢你的回答/建议