宽度和高度必须>使用MPAndroidChart库时出错0

时间:2015-01-09 00:15:23

标签: android android-layout android-bitmap android-cardview mpandroidchart

我想在我的应用程序中使用MPAndroidChartLibrary,但我遇到的问题是我自己无法解决的问题。我想将图表放入存储在ListView中的CardView中。 CardView的xml看起来像这样:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="16dp"
        card_view:cardUseCompatPadding="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/textDateStartTitle"
                android:paddingLeft="5dp"
                android:textSize="26sp"
                android:textColor="@color/accent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:gravity="left"/>

            <com.github.mikephil.charting.charts.PieChart
                android:id="@+id/pie_chart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>

打开包含这些CardViews的片段时,出现错误:

  

java.lang.IllegalArgumentException:width和height必须是&gt; 0               在android.graphics.Bitmap.createBitmap(Bitmap.java:808)               在android.graphics.Bitmap.createBitmap(Bitmap.java:787)               在android.graphics.Bitmap.createBitmap(Bitmap.java:754)               在com.github.mikephil.charting.charts.Chart.onSizeChanged(Chart.java:2197)

当我将android:layout_widthandroid:layout_height设置为com.github.mikephil.charting.charts.PieChart内的某些固定值(例如200dp)时,图表看起来没问题且没有错误。如何在宽度和高度取决于父/内容的情况下摆脱此错误?

2 个答案:

答案 0 :(得分:3)

现在,您的图表高度不依赖于其父LinearLayout的高度:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

请改为尝试:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />

第二组属性将允许PieChart填充容器LinearLayout内可用的所有垂直高度(在考虑其上方TextView的高度后)。

答案 1 :(得分:0)

我找到的唯一方法是在点击按钮时将文件保存附加到图库。之后它完美地运作。我想一旦完全绘制了视图,系统就具有正确的高度和宽度。