选项卡视图中的Achartengine图表

时间:2012-02-10 14:25:13

标签: android-layout tabs achartengine

我是初学者,并且正在使用列表和图表做一些工作。 我使用了与achartengine样品相同的代码。 我的启动器活动派生自TabActivity,其创建方法如下,

setContentView(R.layout.tabstrip);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();  // The activity TabHost
TabHost.TabSpec spec;  // Resusable TabSpec for each tab
Intent intent;  // Reusable Intent for each tab
SalesBarChart myChart = new SalesBarChart();
intent = myChart.execute(this);   
spec = tabHost.newTabSpec("BarChart").setIndicator("BarChart",
                  res.getDrawable(R.drawable.ic_tab_barchart))
              .setContent(intent);
tabHost.addTab(spec);
AverageTemperatureChart myChart1 = new AverageTemperatureChart();
intent = myChart1.execute(this);
spec = tabHost.newTabSpec("albums").setIndicator("Albums",
                  res.getDrawable(R.drawable.ic_tab_linechart))
              .setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(1);

我的xml文件看起来像,

     <TabHost
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
         <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
           <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
           <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp" />
       </LinearLayout>

我在按钮点击的启动性(意图)中触发的相同意图有效,但在标签中不起作用。 请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:1)

两周前我遇到了同样的问题。但我在xml层中的pieChart结合了其他视图。 无论如何,我为每个选项卡和图表创建一个活动,并调用活动而不是图表。 在我的一个活动中,我有这个剪切代码:

public class FirstChartActivity extends Activity{
    // other code
    protected void onResume() {
            super.onResume();
            if (hChartView == null) { // first time pie chart is empty
                renderer = buildCategoryRenderer(colors);
                renderer.setLabelsTextSize(14);
                categorySeries = setCategorySeries("First PieChart",
                        myList);
                // draw pie chart with initialized values (myList and colors)
                hChartView = ChartFactory.getPieChartView(this, categorySeries,
                renderer);
                LinearLayout layout = (LinearLayout) findViewById(R.id.pch_chart);
                layout.addView(hChartView, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
            } else {
                hChartView.repaint();
            }
        }

        protected DefaultRenderer buildCategoryRenderer(int[] colors) {
            DefaultRenderer renderer = new DefaultRenderer();
            for (int color : colors) {
                SimpleSeriesRenderer r = new SimpleSeriesRenderer();
                r.setColor(color);
                renderer.addSeriesRenderer(r);
            }
            return renderer;
        }

        protected CategorySeries setCategorySeries(String chartTitle,
                HashMap<String, Double> hashMap) {
            CategorySeries cs = new CategorySeries(chartTitle);
            Iterator<String> iter = hashMap.keySet().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                Double val = hashMap.get(key);
                cs.add(key, val);
            }
            return cs;
        }
    }

我希望这段代码可以帮助你。

相关问题