【发布时间】:2012-09-26 06:06:36
【问题描述】:
我想实现类似图表的结构。
对于条形图,我使用的是 no。按钮在我的主要活动中,因此输出如下:
然后我正在创建扩展 View 的新类,以便使用画布为图形绘制 x 和 y 轴,如下所示:
public class Draw extends View {
Paint paint = new Paint();
public Draw(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
canvas.drawLine(20, 50, 20, 200, paint);
canvas.drawLine(20, 200, 200, 200, paint);
}
}
但问题是,在我的主要活动中使用以下代码行之后:
Draw draw = new Draw(this);
setContentView(draw);
当我再次设置ContentView 时,我得到以下输出:
所以我的问题是如何整合这两个视图以获得以下输出:
还有其他方法可以做到这一点吗?
感谢任何帮助。
编辑
根据各种建议,我删除了
Draw draw = new Draw(this);
setContentView(draw);
来自我的活动类,而是将这些行放在主 xml 布局中。
<com.example.calci.Draw
android:id="@+id/draw"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
但我正在关注例外:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calci/com.example.calci.CalciActivity}: android.view.InflateException: Binary XML file line #468: Error inflating class com.example.calci.Draw
Caused by: java.lang.NoSuchMethodException: Draw(Context,AttributeSet)
at java.lang.Class.getMatchingConstructor(Class.java:643)
at java.lang.Class.getConstructor(Class.java:472)
at android.view.LayoutInflater.createView(LayoutInflater.java:480)
编辑 2
通过在构造函数中添加AttributeSet attributeSet,我得到了想要的结果。
但是任何人都可以建议我是否应该使用画布中的按钮或矩形来绘制条形图。
请注意,我需要根据seekbar 值动态修改条形图的高度。
【问题讨论】:
-
为什么不在图形的左下角画两条线。
-
在视图类中不可能。我在活动课上用过两次...
-
请检查有问题的 EDIT 2。
标签: android android-layout android-canvas paint bar-chart