Pie AChartEngine不在ScrollView Android中滚动?

时间:2013-10-10 00:38:46

标签: android scrollview achartengine pie-chart

我在android中学习Pie AChartEngine,我想尝试在ScrollView中添加一些馅饼。但这不滚动......如何解决?

这是我的xml

 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
android:layout_height="fill_parent"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/general" >

            <TextView
                android:id="@+id/tv_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="@string/str_tv_title" />

            <LinearLayout
                android:id="@+id/chart_container"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
            </LinearLayout>

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />




        </LinearLayout>
    </ScrollView>

这个java

 private void openChart(){      

    // Pie Chart Slice Names
    code = new String[] {
            "Eclair & Older", "Froyo", "Gingerbread", "Honeycomb",
            "IceCream Sandwich", "Jelly Bean" 
    };      

    // Pie Chart Slice Values
    double[] distribution = { 3.9, 12.9, 55.8, 1.9, 23.7, 1.8 } ;

    // Color of each Pie Chart Slices
    int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.CYAN, Color.RED,
                     Color.YELLOW };

    DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics();
    float val = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, metrics);

    // Instantiating CategorySeries to plot Pie Chart       
    CategorySeries distributionSeries = new CategorySeries(" Android version distribution as on October 1, 2012");
    for(int i=0 ;i < distribution.length;i++){
        // Adding a slice with its values and name to the Pie Chart
        distributionSeries.add(code[i], distribution[i]);
    }   

    // Instantiating a renderer for the Pie Chart
    DefaultRenderer defaultRenderer  = new DefaultRenderer();       
    for(int i = 0 ;i<distribution.length;i++){ 

        // Instantiating a render for the slice
        SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();       
        seriesRenderer.setColor(colors[i]);
        seriesRenderer.setDisplayChartValues(true);

        // Adding the renderer of a slice to the renderer of the pie chart
        defaultRenderer.addSeriesRenderer(seriesRenderer);
    }

    defaultRenderer.setChartTitle("Android version distribution as on October 1, 2012 ");
    defaultRenderer.setChartTitleTextSize(20);
    defaultRenderer.setZoomButtonsVisible(true); 
    defaultRenderer.setLabelsTextSize(val);

    // Getting a reference to view group linear layout chart_container
    LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);


    // Getting PieChartView to add to the custom layout
    mChart = ChartFactory.getPieChartView(getBaseContext(), distributionSeries, defaultRenderer);

    defaultRenderer.setClickEnabled(true);
    defaultRenderer.setZoomEnabled(true);
    defaultRenderer.setSelectableBuffer(10);
    defaultRenderer.setApplyBackgroundColor(true);
    defaultRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
    defaultRenderer.setChartTitleTextSize(20);
    defaultRenderer.setLabelsTextSize(20);
    defaultRenderer.setLegendTextSize(20);
    defaultRenderer.setZoomButtonsVisible(false);
    defaultRenderer.setInScroll(true);
    defaultRenderer.setStartAngle(90);
    defaultRenderer.setPanEnabled(true);

    mChart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();
            if (seriesSelection != null) {

                // Getting the name of the clicked slice
                int seriesIndex = seriesSelection.getPointIndex();
                String selectedSeries="";
                selectedSeries = code[seriesIndex];                 

                // Getting the value of the clicked slice
                double value = seriesSelection.getXValue();
                DecimalFormat dFormat = new DecimalFormat("#.#");

                // Displaying the message
                Toast.makeText(
                        getBaseContext(),
                        selectedSeries + " : "  + Double.valueOf(dFormat.format(value)) + " % " ,
                        Toast.LENGTH_SHORT).show();
            }
    }
    });

    // Adding the pie chart to the custom layout
    chartContainer.addView(mChart);
}

我在ScrollView中添加了 android:fillViewport =“true”,我在java中也添加了 defaultRenderer.setInScroll(true); ,但它不起作用..

我希望有人能帮助我。对不起,如果我的英语不好......

2 个答案:

答案 0 :(得分:0)

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/general" >

尝试将android:layout_height设置为wrap_content

答案 1 :(得分:0)

mRenderer.setZoomEnabled(假);

mRenderer.setPanEnabled(假);

mRenderer.setZoomRate(6.0f);

mRenderer.setShowLabels(真);

mRenderer.setFitLegend(真);

mRenderer.setInScroll(真);

这对我有用。