我需要动态创建框架布局和周围布局。使用FragmentManager
,您必须使用布局标识符来添加片段。
动态创建布局时,它没有标识符。
FrameLayout frameLayout = new FrameLayout(this);
MyFragment fragment = new MyFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.??????, fragment)
.commit();
我在这里挣扎。我可以动态生成标识符并使用frameLayout.setId()
进行分配吗?还有其他选择吗?
答案 0 :(得分:2)
是的,一种选择是使用frameLayout.setId()
。要获得正确的id,可以在ids.xml文件中定义id。另见:http://developer.android.com/guide/topics/resources/more-resources.html#Id
另一个选择是让一个xml布局文件只有一个带有id的FrameLayout,然后你膨胀并使用那里定义的id。
这两种方法都允许您使用R.id.xxx