开发者

create layout through 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);

     }
}

Here output screen contains three charts but i want to position third chart i开发者_如何学Gon the second line. Please help me to solve this problem. I am beginner in Android.


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

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>

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


Are you completely sure that you can't do it with an XML? Coding gui from the code is quite more difficult.

In relation to your question: I guess you want the third GraphicalView to be to the right of the second one.

Two ways to do it: Using a RelativeLayout or using a second LinearLayout.

Example:

LinearLayout layout = new LinearLayout(this);
layout.addView(gView, 150, 200);

LinearLayout two = new LinearLayout(this);
two.setOrientation(LinearLayout.VERTICAL);
two.addView(gView2, 150, 200);
two.addView(gView3, 150, 150);

layout.addView(two);

Try not using that way of giving size to a View, you should use setLayoutParams.

For instance your layout should have something like:

layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜