本文介绍了LinearLayout 内的 TChart 生成“宽度和高度必须>"0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在 Mono for Android 上使用 Steema TChart.我试过这个简单的代码,效果很好:

I have just started using Steema TChart on Mono for Android. I tried this simple code and it works fine:

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    var tChart1 = new Steema.TeeChart.TChart(this);
    var bar1 = new Steema.TeeChart.Styles.Bar();
    tChart1.Series.Add(bar1);
    bar1.Add(3, "Pears", Color.Red);
    bar1.Add(4, "Apples", Color.Blue);
    bar1.Add(2, "Oranges", Color.Green);
    var theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart);
    theme.Apply();

    SetContentView(tChart1);
}

...但是一旦我尝试将图表添加到另一个视图中,例如 LinearLayout,我就会收到错误宽度和高度必须 > 0".有大佬知道怎么解决吗?

...but as soon as I try adding the chart into another view, like a LinearLayout I get the error "width and height must be > 0". Does anyone know how to solve this?

这是我尝试过的代码(如果我只有 LinearLayout 和按钮,它就可以正常工作):

Here is the code that I tried (it works just fine if I only have the LinearLayout and the Button):

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    var tChart1 = new Steema.TeeChart.TChart(this);
    var bar1 = new Steema.TeeChart.Styles.Bar();
    tChart1.Series.Add(bar1);
    bar1.Add(3, "Pears", Color.Red);
    bar1.Add(4, "Apples", Color.Blue);
    bar1.Add(2, "Oranges", Color.Green);
    var theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart);
    theme.Apply();

    var container = new LinearLayout(this);
    container.Orientation = Orientation.Vertical;
    var b = new Button(this);
    b.Text = "Test";
    container.AddView(b);
    container.AddView(tChart1);

    SetContentView(container);
}

这是调用堆栈:

08-29 19:39:52.489 I/MonoDroid(24907): java.lang.IllegalArgumentException: width and height must be > 0
08-29 19:39:52.489 I/MonoDroid(24907):  at android.graphics.Bitmap.createBitmap(Bitmap.java:603)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.graphics.Bitmap.createBitmap(Bitmap.java:585)
08-29 19:39:52.489 I/MonoDroid(24907):  at steema.teechart.TChart.n_onDraw(Native Method)
08-29 19:39:52.489 I/MonoDroid(24907):  at steema.teechart.TChart.onDraw(TChart.java:55)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.View.draw(View.java:11025)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.View.draw(View.java:11028)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-29 19:39:52.489 I/MonoDroid(24907):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2274)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewRootImpl.draw(ViewRootImpl.java:2041)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1647)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2459)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.os.Looper.loop(Looper.java:137)
08-29 19:39:52.489 I/MonoDroid(24907):  at android.app.ActivityThread.main(ActivityThread.java:4514)
08-29 19:39:52.489 I/MonoDroid(24907):  at java.lang.reflect.Method.invokeNative(Native Method)
08-29 19:39:52.489 I/MonoDroid(24907):  at java.lang.reflect.Method.invoke(Method.java:511)
08-29 19:39:52.489 I/MonoDroid(24907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
08-29 19:39:52.489 I/MonoDroid(24907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
08-29 19:39:52.489 I/MonoDroid(24907):  at dalvik.system.NativeStart.main(Native Method)

推荐答案

这是因为你需要提供 LayoutParams 将图表添加到您的视图时,例如:

This is because you need to provide LayoutParams when adding the chart to your View, for example:

container.AddView(tChart1, new ViewGroup.LayoutParams(300, 400));

container.AddView(tChart1, new ViewGroup.LayoutParams(300, 400));

或:

container.AddView(tChart1, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));

container.AddView(tChart1, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));

这篇关于LinearLayout 内的 TChart 生成“宽度和高度必须>"0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:36