2

我有线性布局,我想将 FrameLayout 膨胀到其中。你知道,它是怎么做到的吗?可能吗?我仍然收到“没有找到适合 Inflate 的方法”的错误

谢谢

编辑:回答自己:

LinearLayout ll=(LinearLayout) findViewById(R.id.linear);
final LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout frml = (FrameLayout)inflater.inflate(R.layout.frame,null);
frml.setId(10101010);
frml.setBackgroundColor(Color.RED);

ll.addView(frml);
4

2 回答 2

2

要获取LayoutInflater实例,您需要将其作为这样的服务获取

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

然后你可以用它来膨胀FrameLayout并像这样将它添加到LinearLayout

LinearLayout linearLayout = ... ;
inflater.inflate(R.layout.your_framelayout_file_name, linearLayout, false);

并且不要忘记像这样为您的FrameLayout放置布局宽度和高度

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    .
    .
    .
</FrameLayout>

有关LayoutInflater的更多信息, 请访问此链接

于 2011-10-27T13:07:44.100 回答
0
LinearLayout parent = ...
LayoutInflater li = LayoutInflater.from(context);
View view = li.inflate(R.layout.my_layout, parent, false);
parent.addView(view);
于 2011-10-27T09:36:45.487 回答