ScrollView无法使用aChartEngine

时间:2014-08-18 19:38:47

标签: android layout scrollview achartengine

我正在尝试将多个图表放在一个活动上,并使用滚动视图查看它们。

前两个图表正在显示,但后两个图表没有显示。我已经按照教程中的所有说明进行操作,并且已经按照堆栈上的所有建议进行操作,但它们都没有工作。

我也尝试删除滚动视图,但图表仍未显示。任何帮助将不胜感激。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >



    <ScrollView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true"
>

        <LinearLayout 
            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">


    <LinearLayout
    android:id="@+id/chart"
    android:layout_width="fill_parent"
    android:layout_height="300dp" 
    android:orientation="horizontal" />

    <LinearLayout
    android:id="@+id/chart1"
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:layout_below="@id/chart"
    android:orientation="horizontal" />

     <LinearLayout
    android:id="@+id/chart2"
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:layout_below="@id/chart1"
    android:orientation="horizontal" />

      <LinearLayout
    android:id="@+id/chart3"
    android:layout_width="fill_parent"
    android:layout_height="300dp"
    android:layout_below="@id/chart2"
    android:orientation="horizontal" />

     </LinearLayout>

     </ScrollView>

</RelativeLayout>

Linegraph.java

public class LineGraph extends Activity{

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();    
    protected static GraphicalView mChartView;

    public GraphicalView getView(Context context, int[]z, int []zz){

        CategorySeries series = new CategorySeries("line1");


    for(int i=0;i<z.length;i++){
        series.add("Bar " + (i+1),z[i]);
    }


    CategorySeries series2= new CategorySeries("demo");
    for(int i=0;i<zz.length;i++){
        series2.add("Bar " + (i+1),zz[i]);
    }

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    dataset.addSeries(series.toXYSeries());
    dataset.addSeries(series2.toXYSeries());

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
    mRenderer.setAxesColor(Color.GREEN);

    XYSeriesRenderer renderer = new XYSeriesRenderer();
    renderer.setDisplayChartValues(true);
    renderer.setChartValuesSpacing((float)0.5);
    mRenderer.addSeriesRenderer(renderer);

    mRenderer.setZoomEnabled(false);
    mRenderer.setPanEnabled(false);
    mRenderer.setZoomRate(6.0f);
    mRenderer.setShowLabels(true);
    mRenderer.setFitLegend(true);
    mRenderer.setInScroll(true); 


    XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    renderer.setColor(Color.CYAN);
    renderer.setDisplayChartValues(true);
    mRenderer.addSeriesRenderer(renderer2);




        return ChartFactory.getLineChartView(context, dataset, mRenderer);



    }




}

MainActivity:

public class MainActivity extends LineGraph {

    int a[] ={1,2,3};
    int b[] ={4,5,6};
    int c[] ={7,8,9};
    int d[] ={10,11,12};
    int e[] ={13,14,15};
    int f[] ={16,17,18};
    int g[] ={19,20,21};
    int h[] ={22,23,24};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        lineGraphHandler ();

    }




    public void lineGraphHandler (){
        LineGraph line = new LineGraph();
        GraphicalView gView = line.getView(this, a,b);
        GraphicalView gV = line.getView(this,c,d);
        GraphicalView gV2 = line.getView(this,e,f);
        GraphicalView gV3 = line.getView(this,g,h);

        LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
        layout.addView(gView);      

        LinearLayout layout2 = (LinearLayout) findViewById(R.id.chart1);
        layout2.addView(gV);

        LinearLayout layout3 = (LinearLayout) findViewById(R.id.chart2);
        layout2.addView(gV2);

        LinearLayout layout4 = (LinearLayout) findViewById(R.id.chart3);
        layout2.addView(gV3);
    }



}

2 个答案:

答案 0 :(得分:1)

使用滚动视图作为父布局而不是相对布局

答案 1 :(得分:0)

您无法在ScrollView内滚动:

正如我上面所见,你使用

 <ScrollView>
 </ScrollView> 

在嵌入式布局中,其中包含您需要滚动的图表,遗憾的是嵌套滚动似乎不起作用。