通过code创建布局 [英] create layout through code

查看:121
本文介绍了通过code创建布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过code创建布局,这样我可以使用下面code加上该布局的多个视图。

I created layout through code so that i can add multiple views on that layout using code below.

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系统。

Here output screen contains three charts but i want to position third chart in the second line. Please help me to solve this problem. I am beginner in Android.

推荐答案

您可以通过嵌套多个的LinearLayout 和改变方向属性实现这个

You can achieve this by nesting multiple LinearLayouts and changing the orientation property

在XML中,这将是这个样子(只显示相关的元素和属性):

in XML this would look something like this (showing only the relevant elements and attributes):

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

您可以通过编程方式使用<一个操纵的LinearLayout 的方向href=\"http://developer.android.com/reference/android/widget/LinearLayout.html#setOrientation%28int%29\"相对=nofollow> setOrientation方法。例如:

You can programmatically manipulate the orientation of the LinearLayout by using the setOrientation method. E.g:

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

这篇关于通过code创建布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆