繁体   English   中英

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

[英]How to inflate another view into LinearLayout?

这是我的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);

然后膨胀graphViewll_one ,和graphView2ll_two

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

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

但它没有inflate()方法。

您不需要在LinearLayoutinflate另一个view ,您只需要在该LinearLayout添加另一个子view 只需在LinearLayout上使用最适合您的addView()方法。 这是一个例子:

   llOne.addView(graphView);

如果扩展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>

试试这种方式我已经将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