简体   繁体   English

引用以编程方式添加的Framelayout(Android)

[英]Reference a programmatically added Framelayout (Android)

I'm new to android development so this might be pretty straightforward. 我是android开发的新手,所以这可能很简单。 However, I couldn't find anything that worked for me. 但是,我找不到对我有用的任何东西。

I've managed to successfully add multiple framelayouts to a linear layout through the following code: 我设法通过以下代码成功地将多个框架布局添加到线性布局:

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);


    }



}

So at some point later in the program I'd like to reference these framelayouts and I figured since I knew the IDs I could just use getViewById. 因此,在程序的稍后部分,我想引用这些框架布局,由于我知道可以只使用getViewById的ID,所以我认为。

This doesn't seem to work though. 不过,这似乎不起作用。 I've only slightly modified the above code by adding a redundant FrameLayout object called testFrame as below: 我仅通过添加一个称为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

    }



}

So basically, it shows none of the framelayouts anymore because the testFrame object is now null. 所以基本上,它不再显示任何帧布局,因为testFrame对象现在为null。 Any ideas on what I'm doing wrong? 关于我在做什么错的任何想法吗?

Thanks to Luksprog for the answer. 感谢Luksprog的回答。 Simple creating an element and assigning an id to it is not enough. 仅创建元素并为其分配ID是不够的。 It needs to be added to an existing view that is referenced in the R file. 需要将其添加到R文件中引用的现有视图中。 In my case the existing view was R.id.fragment_container 就我而言,现有视图是R.id.fragment_container

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM