目标:滚动视图中的多个图表(首先只是垂直可滚动,但在具有 2 列的 Gridview 中首选)
问题:垂直滚动不起作用
提示:图表的大小(使用 onMeasure)和 fillViewport = true 有效
从 stackoverflow 尝试了很多解决方案(使用 wrap_content、match_parent、weights 等),但没有成功。
MPAndroidChart 的变化
mChart.setRotationEnabled(false);
mChart.setTouchEnabled(false);
Disabled OnChartValueSelectedListener, SeekBar.OnSeekBarChangeListener
布局文件
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:elevation="@dimen/elevation"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:fillViewport="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:weightSum="100"
tools:context=".MainActivity">
<LinearLayout android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75"
android:orientation="vertical">
<MPAndroidChart.PieChartSquare
android:id="@+id/chart1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout android:id="@+id/linearlayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75"
android:orientation="vertical">
<MPAndroidChart.PieChartSquare
android:id="@+id/chart2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.DrawerLayout>
MPAndroidChart.PieChartSquare 从原始 PieChart 扩展,但我添加了以下代码来调整字符大小。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);
w = Math.min(w, h);
h = w;
setMeasuredDimension(w, h);
}
谢谢并恭祝安康
更新
我使用自定义 Gridview 完成了这个技巧,然后为每个图表添加了一个 chartitem.xml。不幸的是,PieChartSquare 不起作用(什么都看不见),所以我必须使用 PieChart。
所以我没有得到正确的图表大小。它总是非常非常小。