实现aChartEngine时ScrollView无法正常工作

时间:2015-02-27 10:55:52

标签: android uiscrollview achartengine

在我的应用程序在单个屏幕上我试图显示多个图形,所以为此目的我添加scrollView到布局。但是,只要我添加scrollView,条形图就不会显示出来。

graph_activity.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">

    <Button
        android:id="@+id/btn_chart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:text="Draw chart" />
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btn_chart"
        tools:ignore="Orientation" >
    </LinearLayout>
    </ScrollView>

</RelativeLayout>

GraphActivity.Java 文件是

public class ChartMainActivity extends Activity {

    private View mChart;
    private String[] mMonth = new String[] {
            "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
            "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_sample_chart);
        Button btnChart = (Button) findViewById(R.id.btn_chart);
        OnClickListener clickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                openChart();
            }
        };
        btnChart.setOnClickListener(clickListener);
    }

    private void openChart(){
        int[] x = { 0,1,2,3,4,5,6,7, 8, 9, 10, 11 };
        int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800, 0,0,0,0};

        XYSeries incomeSeries = new XYSeries("Income");
        XYSeries expenseSeries = new XYSeries("Expense");
        for(int i=0;i<x.length;i++){
            incomeSeries.add(i,income[i]);
        }

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        dataset.addSeries(incomeSeries);
        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
        incomeRenderer.setColor(Color.CYAN); //color of the graph set to cyan
        incomeRenderer.setFillPoints(true);
        incomeRenderer.setLineWidth(2);
        incomeRenderer.setDisplayChartValues(true);
        incomeRenderer.setDisplayChartValuesDistance(10); //setting chart value distance

        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setOrientation(XYMultipleSeriesRenderer.Orientation.HORIZONTAL);
        multiRenderer.setXLabels(0);
        multiRenderer.setChartTitle("Income vs Expense Chart");
        multiRenderer.setXTitle("Year 2014");
        multiRenderer.setYTitle("Amount in Dollars");
        multiRenderer.setApplyBackgroundColor(true);

        for(int i=0; i< x.length;i++){
            multiRenderer.addXTextLabel(i, mMonth[i]+"\t");
        }
        LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart);
        chartContainer.removeAllViews();
        mChart = ChartFactory.getBarChartView(ChartMainActivity.this, dataset, multiRenderer,Type.DEFAULT);
        chartContainer.addView(mChart);

    }
}

如果我删除了scrollView,则会显示条形图,一切正常。在放置scrollView我不知道为什么条形图没有显示。相反,空白布局即将到来。

0 个答案:

没有答案