我通过代码创建了布局,以便可以使用下面的代码在该布局上添加多个视图。
public class AndroidPrepChart extends Activity {
GraphicalView gView;
GraphicalView gView2;
GraphicalView gView3;
BarChart barChart = new BarChart();
BarChart2 barChart2 = new BarChart2();
BarChart3 barChart3 = new BarChart3();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gView = barChart.execute2(this);
gView2 = barChart2.execute2(this);
gView3 = barChart3.execute2(this);
LinearLayout layout = new LinearLayout(this);
layout.addView(gView, 150, 200);
layout.addView(gView2, 150, 200);
layout.addView(gView3, 150, 150);
setContentView(layout);
}
}
这里输出屏幕包含三个图表,但我想把第三个图表放在第二行。请帮我解决这个问题。我是Android的初学者。
最佳答案
可以通过嵌套多个LinearLayout
s并更改“方向”属性来实现此目的。
在XML中,这看起来像这样(仅显示相关的元素和属性):
<LinearLayout android:orientation="vertical">
<LinearLayout android:orientation="horizontal">
<!-- Your first 2 graphs go in this LinearLayour -->
</LinearLayout>
<LinearLayout android:orientation="horizontal">
<!-- The third graph goes in here -->
</LinearLayout>
</LinearLayout>
您可以使用setOrientation method以编程方式操纵
LinearLayout
的方向。例如:LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
关于android - 通过代码创建布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3271215/