问题
I've being trying to display two graphics in one screen but I'm not able. Here is the code where i show one graphic inside the onClick function:
case R.id.graphs:
GraphicalView mChartView = new Niveau().execute(this);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph);
layout.removeAllViews();
layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
break;
Any idea??
Thank you very much!
回答1:
case R.id.graphs:
GraphicalView mChartView1 = new Niveau().execute(this);
GraphicalView mChartView2 = // another chartView creation
LinearLayout layout = (LinearLayout) findViewById(R.id.graph);
layout.removeAllViews();
layout.addView(mChartView1, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(mChartView2, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
break;
That should work. Be sure to have different ids on your mChartView
- if you create them programmatically and not inflated from an XML layout, you need to set the id programmatically too:
mChartView1.setId(1);
mChartView2.setId(2);
Also, a RelativeLayout
would probably offer you more customization in term of layout and positionning. You then need to set the relative positions in your layout params:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.graph);
....
RelativeLayout.LayoutParams paramsForChart2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
paramsForChart2.addRule(RelativeLayout.RIGHT_OF, mChartView1.getId());
layout.addView(mChartView2, paramsForChart2);
回答2:
Before adding the second layout you have to call layout.removeAllViews();
so that your Layout is now free to show another layout inside it. So try to remove first and then add the second layout.
来源:https://stackoverflow.com/questions/8383410/two-graphics-in-one-screen