本文介绍了宽度和高度必须是> 0错误使用MPAndroidChart库时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中使用MPAndroidChartLibrary,但我遇到了,我不能在我自己解决问题。我想在图表成存储里面的ListView的CardView。 CardView的XML看起来是这样的:

I wanted to use MPAndroidChartLibrary in my application, but I am experiencing problems which I can't solve on my own. I would like to put the chart into the CardView which is stored inside ListView. CardView's xml looks like this:

<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我得到一个错误:

When opening the fragment containing these CardViews I get an error:

java.lang.IllegalArgumentException异常:宽度和高度必须> 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_width 的android:layout_height 里面的 com.github.mikephil.charting.charts.PieChart 图表看起来不错,并没有错误。而其宽度和高度取决于父母/内容如何摆脱这个错误的?

When I set android:layout_width and android:layout_height to some fixed values (eg. 200dp) inside com.github.mikephil.charting.charts.PieChart the chart looks ok and there is no error. How to get rid of this error while having width and height depending on the parent/content?

推荐答案

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

Right now, your chart height does not depend on the height of its parent 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"
            />

第二组的属性将让你的饼图来填充容器内的所有可用的垂直高度的LinearLayout (占的高度后,的TextView 上面)。

The second set of attributes will allow your PieChart to fill all the vertical height available inside the container LinearLayout (after accounting for the height of the TextView above it).

这篇关于宽度和高度必须是&GT; 0错误使用MPAndroidChart库时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:36