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

时间:2014-04-26 10:41:53

标签: android android-layout android-fragments android-framelayout

我是Android开发的新手,所以这可能非常简单。但是,我找不到任何对我有用的东西。

我通过以下代码成功地将多个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);




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


    }



}

所以在稍后的程序中,我想引用这些framelayouts,我想,因为我知道ID,我可以使用getViewById。

这似乎不起作用。我通过添加一个名为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

    }



}

基本上,它不再显示任何framelayout,因为testFrame对象现在为null。关于我做错什么的任何想法?

1 个答案:

答案 0 :(得分:0)

感谢Luksprog的回答。简单地创建元素并为其分配id是不够的。它需要添加到R文件中引用的现有视图中。在我的例子中,现有的视图是R.id.fragment_container