2012-02-10 88 views
0

我是一名初學者,正在做一些列表和圖表。 我使用了與achartengine樣本相同的代碼。 我的發射活動,這是從TabActivity獲得有其創建方法如下,標籤視圖中的Achartengine圖表

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> 

同樣的意圖我朝四周startactivity(意向)按鈕點擊工程,但不能在標籤中工作。 請幫忙。提前致謝。

回答

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; 
     } 
    } 

我希望這個代碼可以幫助您。