我需要在我的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);
當您嘗試此操作時會發生什麼?顯示的結果是否意外或您是否遇到異常? – Nick 2014-09-10 17:19:01
尼克,我只看到空白屏幕......我沒有得到任何例外。 – Nik 2014-09-10 17:36:02