[英]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.