[英]Reference a programmatically added Framelayout (Android)
我是android开发的新手,所以这可能很简单。 但是,我找不到对我有用的任何东西。
我设法通过以下代码成功地将多个框架布局添加到线性布局:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout fragmentContainer=(LinearLayout) findViewById(R.id.fragment_container);
fragmentContainer.setOrientation(LinearLayout.VERTICAL);
//if (findViewById(R.id.fragment_container) != null) {
for(int i=1;i<10;i++){
//create a new TextFrag object
TextFrag textFragment=new TextFrag();
//pass arguments to the textFrag Object with the Fragment Number
Bundle args=new Bundle();
args.putInt("FragNumber", i);
textFragment.setArguments(args);
//Create a new Frame Layout for Each Fragment. This is useful if you want to switch fragments at runtime
//Dont know exact details yet
FrameLayout fragmentFrame=new FrameLayout(this);
//set a unique id for the FrameLayout
fragmentFrame.setId(i);
//Define the Frame Layout Params
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//Add the Fragment to the Frame Layout
getSupportFragmentManager().beginTransaction().add(fragmentFrame.getId(),textFragment).commit();
//Add the Frame Layout to the Linear Layout
fragmentContainer.addView(fragmentFrame);
}
}
因此,在程序的稍后部分,我想引用这些框架布局,由于我知道可以只使用getViewById的ID,所以我认为。
不过,这似乎不起作用。 我仅通过添加一个称为testFrame的冗余FrameLayout对象对上述代码进行了少许修改,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout fragmentContainer=(LinearLayout) findViewById(R.id.fragment_container);
fragmentContainer.setOrientation(LinearLayout.VERTICAL);
//if (findViewById(R.id.fragment_container) != null) {
for(int i=1;i<10;i++){
//create a new TextFrag object
TextFrag textFragment=new TextFrag();
//pass arguments to the textFrag Object with the Fragment Number
Bundle args=new Bundle();
args.putInt("FragNumber", i);
textFragment.setArguments(args);
//Create a new Frame Layout for Each Fragment. This is useful if you want to switch fragments at runtime
//Dont know exact details yet
FrameLayout fragmentFrame=new FrameLayout(this);
//set a unique id for the FrameLayout
fragmentFrame.setId(i);
//Define the Frame Layout Params
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//CHANGED CODE STARTS HERE
int theId=i;
FrameLayout testFrame=(FrameLayout) findViewById(theId);
if(testFrame!=null){
//Add the Fragment to the Frame Layout
getSupportFragmentManager().beginTransaction().add(testFrame.getId(),textFragment).commit();
//Add the Frame Layout to the Linear Layout
fragmentContainer.addView(testFrame);
}
//CHANGED CODE ENDS HERE
}
}
所以基本上,它不再显示任何帧布局,因为testFrame对象现在为null。 关于我在做什么错的任何想法吗?
感谢Luksprog的回答。 仅创建元素并为其分配ID是不够的。 需要将其添加到R文件中引用的现有视图中。 就我而言,现有视图是R.id.fragment_container
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.