简体   繁体   中英

How to inflate another view into LinearLayout?

This is my XML with 2 LinearLayouts .

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <LinearLayout
            android:id="@+id/ll_one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
    <LinearLayout
            android:id="@+id/ll_two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            />
</LinearLayout>

I have downloaded GraphView from http://android.arnodenhond.com/components/graphview . This class extends View. I basically want to initialize 2 graphs via

String[] verlabels = new String[]{"aim", "25%", "50%", "75%", "start"};
String[] horlabels = new String[]{"this", "max"};

float[] values = new float[]{2.0f, 6.0f};
aaaGraphView graphView = new aaaGraphView(this, values, "Graph One",
        horlabels, verlabels, aaaGraphView.BAR);

float[] values2 = new float[]{1.0f, 12.0f};
aaaGraphView graphView2 = new aaaGraphView(this, values2, "Graph Two",
        horlabels, verlabels, aaaGraphView.BAR);

and then to inflate graphView into ll_one , and graphView2 into ll_two .

How should I do this? I have initialized the LinearLayout

   llOne = (LinearLayout) findViewById(R.id.ll_one);

but it does not have inflate() method.

You do not need to inflate another view inside your LinearLayout you'll just have to add another child inside that LinearLayout . Just use the addView() method on the LinearLayout that fits you the best. Here is an example :

   llOne.addView(graphView);

If the name of your class which extends View is aaaGraphView try this:

<package1.package2....aaaGraphView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</package1.package2....aaaGraphView>

try this way i have added the linearlayout into another linear layout this way,

setContentView(R.layout.main);
final ViewGroup vg = (ViewGroup) findViewById(R.id.ll_one);
vg.addView(graphView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM