简体   繁体   English

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

[英]How to inflate another view into LinearLayout?

This is my XML with 2 LinearLayouts . 这是我的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>

I have downloaded GraphView from http://android.arnodenhond.com/components/graphview . 我从http://android.arnodenhond.com/components/graphview下载了GraphView。 This class extends View. 这个类扩展了View。 I basically want to initialize 2 graphs via 我基本上想要初始化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);

and then to inflate graphView into ll_one , and graphView2 into ll_two . 然后膨胀graphViewll_one ,和graphView2ll_two

How should I do this? 我该怎么做? I have initialized the LinearLayout 我初始化了LinearLayout

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

but it does not have inflate() method. 但它没有inflate()方法。

You do not need to inflate another view inside your LinearLayout you'll just have to add another child inside that LinearLayout . 您不需要在LinearLayoutinflate另一个view ,您只需要在该LinearLayout添加另一个子view Just use the addView() method on the LinearLayout that fits you the best. 只需在LinearLayout上使用最适合您的addView()方法。 Here is an example : 这是一个例子:

   llOne.addView(graphView);

If the name of your class which extends View is aaaGraphView try this: 如果扩展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>

try this way i have added the linearlayout into another linear layout this way, 试试这种方式我已经将linearlayout添加到另一个线性布局中,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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