2014-09-10 30 views
1

我需要在我的Activity上創建一些PieCharts,並且圖表的數量會有所不同。 我嘗試從代碼動態創建PieChart,但它不起作用。我的佈局正確 - 按鈕已成功添加。你可以幫我嗎?有些代碼: 佈局:Androidplot - 創建沒有XML的圖表

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
tools:context="example.bros.nik.tabs1.MealsActivity" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/l_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:orientation="vertical" > 
</LinearLayout> 
</ScrollView> 

的Java:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_meals); 
    LinearLayout l_layout = (LinearLayout) findViewById(R.id.l_layout); 
    meals_params = ResultsActivity.get_meals_params();   

    LinearLayout.LayoutParams lp_view = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f); 

    PieChart pie = new PieChart(this, "Chart#1"); 

    Segment s1 = new Segment("Углеводы", meals_params[0].get_carbohydrates_percent()); 
    Segment s2 = new Segment("Жиры", meals_params[0].get_fats_percent()); 
    Segment s3 = new Segment("Белки", meals_params[0].get_proteins_percent()); 

    EmbossMaskFilter emf = new EmbossMaskFilter(new float[]{1, 1, 1}, 0.4f, 10, 8.2f); 

    SegmentFormatter sf1 = new SegmentFormatter(); 
    sf1.configure(getApplicationContext(), R.xml.pie_segment_formatter1); 
    sf1.getFillPaint().setMaskFilter(emf); 

    SegmentFormatter sf2 = new SegmentFormatter(); 
    sf2.configure(getApplicationContext(), R.xml.pie_segment_formatter2); 
    sf2.getFillPaint().setMaskFilter(emf); 

    SegmentFormatter sf3 = new SegmentFormatter(); 
    sf3.configure(getApplicationContext(), R.xml.pie_segment_formatter3); 
    sf3.getFillPaint().setMaskFilter(emf); 

    pie.addSeries(s1, sf1); 
    pie.addSeries(s2, sf2); 
    pie.addSeries(s3, sf3); 


    pie.getRenderer(PieRenderer.class).setDonutSize(0.1f,PieRenderer.DonutMode.PERCENT); 
    pie.redraw(); 
    pie.getBorderPaint().setColor(Color.TRANSPARENT); 
    pie.getBackgroundPaint().setColor(Color.TRANSPARENT); 

    l_layout.addView(pie, lp_view); 

我試試這個:

pie.setLayoutParams(lp_view); 
l_layout.addView(pie); 

,它是不會幫我。

如果我添加按鈕佈局:

pie.setLayoutParams(lp_view); 
l_layout.addView(pie); 

Button but = new Button(this); 
but.setText("bla"); 
l_layout.addView(but, lp_view); 

然後我看到這個按鈕(看截圖)。 Added button

編輯: 在Android docs所寫的,在構造

ViewGroup.LayoutParams (int width, int height) 

屬性可以是視絕對尺寸,或可爲常量 - WRAP_CONTENT(int值-2),MATCH_PARENT(int值 - 1)。很可能,PieChart不會識別這些常量,並將其作爲絕對大小的負值。 我找到了解決辦法:

private int[] getDisplayParams() { 
    int[] display_params = new int[2]; 
    Display display = getWindowManager().getDefaultDisplay(); 
    if (Build.VERSION.SDK_INT >= 13) { 
     Point size = new Point(); 
     display.getSize(size); 
     display_params[0] = size.x; 
     display_params[1] = size.y; 
    } 
    else { 
     display_params[0] = display.getWidth(); 
     display_params[1] = display.getHeight(); 
    } 
    return display_params; 
} 

和屏幕的大小使用確定表的大小:

int[] display_params = getDisplayParams(); 
Double pie_size = display_params[0]*0.8; 
int pie_size_int = pie_size.intValue(); 
ViewGroup.LayoutParams lp_view = new LinearLayout.LayoutParams(pie_size_int, pie_size_int); 
+0

當您嘗試此操作時會發生什麼?顯示的結果是否意外或您是否遇到異常? – Nick 2014-09-10 17:19:01

+0

尼克,我只看到空白屏幕......我沒有得到任何例外。 – Nik 2014-09-10 17:36:02

回答

1

看起來你是不是提供了餡餅的LayoutParams。嘗試添加餡餅l_layout之前這樣做:

pie.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT, 
    LinearLayout.LayoutParams.FILL_PARENT)); 

編輯: 我回去,發現該佈局被封閉在一個滾動型,但滾動視圖設置爲包裹內容和內容的設置與父所以似乎它可能是一個雞與雞蛋問題as described here。我相信按鈕等有這些默認非零值,而陰謀不,這可能解釋爲什麼按鈕的作品。

作爲一個全面的檢查,如果你指定你的情節像這樣的絕對大小發生了:

LinearLayout.LayoutParams lp_view = new LinearLayout.LayoutParams(100, 100); 

你還可以嘗試:

plot.setMinimumHeight(100); 
plot.setMinimumWidth(100); 
+0

不,我在這裏提供餅圖的LayoutParams:[l_layout.addView(pie,lp_view);] - 查看第二個參數。不過,我嘗試了你的建議,但這對我沒有幫助。我編輯我的帖子,看看這個。 – Nik 2014-09-11 09:40:12

+0

我嘗試在LayoutParams中爲PieChart使用FILL_PARENT,MATCH_PARENT,WRAP_CONTENT參數。沒有結果。 – Nik 2014-09-11 09:41:42

+0

我已經更新了我的答案。 – Nick 2014-09-11 14:01:01

1

問題由尼克解決。 您也可以嘗試:

plot.setMinimumHeight(100); 
plot.setMinimumWidth(100);