如何将另一个视图膨胀为LinearLayout?

时间:2011-10-17 10:07:20

标签: android graph android-linearlayout android-inflate layout-inflater

这是我的2 LinearLayouts的XML。

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

我已从http://android.arnodenhond.com/components/graphview下载了GraphView。这个类扩展了View。我基本上想通过

初始化2个图表
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);

然后将graphView扩展为ll_one,将graphView2扩展为ll_two

我该怎么做?我初始化了LinearLayout

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

但它没有inflate()方法。

3 个答案:

答案 0 :(得分:5)

您需要在inflateview另外LinearLayout,只需要在LinearLayout内添加另一个孩子。只需使用最适合您的LinearLayout addView()方法即可。这是一个例子:

   llOne.addView(graphView);

答案 1 :(得分:3)

如果扩展View的类的名称是aaaGraphView,请尝试:

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

答案 2 :(得分:3)

尝试这种方式我已经将linearlayout以这种方式添加到另一个线性布局中,

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