Setting attribute of child element of included layout

后端 未结 3 1714
情书的邮戳
情书的邮戳 2021-02-03 18:46

I have a main.xml file describing the layout of my main activity:




        
3条回答
  •  眼角桃花
    2021-02-03 19:15

    No, there is no way to pass parameters to the included layout other than the layout params using the directive.

    You can inflate the layout programatically and add them to your view. Add an id to the container in your main layout:

    
    
    

    Then in your Activity:

    ViewGroup container = (ViewGroup)findViewById(R.id.container);
    for (int i = 0; i < 6; i++) {
        View myLayout = getLayoutInflater.inflate(R.layout.mylayout, null);
        TextView tv = myLayout.findViewById(R.id.textView);
        tv.setText("my layout " + i);
        container.addView(myLayout); // you can pass extra layout params here too
    }
    

提交回复
热议问题