Is there a way to programmatically create copies of a layout in android?

后端 未结 2 503
我在风中等你
我在风中等你 2020-12-21 22:09

The main goal is to use a pre-made layout to create separate modules that can be edited, and then programmatically add them to the root layout. To clarify, several modules s

相关标签:
2条回答
  • 2020-12-21 22:14

    Use inflater to create a view from resource. Then you can add it programmatically

    context.LayoutInflater.Inflate(Resource.Layout.oneimg_twolbl, null);
    
    0 讨论(0)
  • 2020-12-21 22:27

    Lets assume you have a LinearLayout with an orientation of vertical in your main axml that you wish to attach multiple views to.

    Get a reference to that "parent" LinearLayout:

    var linearLayoutParent = FindViewById<LinearLayout>(Resource.Id.linearLayout1);
    

    Then in some loop, use LayoutInflater.Inflate to inflate your repeating layout, use the view returned and FindViewById on that View each of the elements you need to update and then add that view to the parent view with an increasing index:

    index++;
    var view = LayoutInflater.Inflate(Resource.Layout.RepeatingLayout, linearLayoutParent, false);
    var letter = view.FindViewById<TextView>(Resource.Id.textView1);
    letter.Text = index.ToString();
    // FindViewById for textView2, textView3 and assign the text on each....
    linearLayoutParent.AddView(view, index);
    

    Note: If you have a lot of these repeating elements and you will have to scroll them (off screen), look at using a RecyclerView instead, it will save you a lot of headaches into terms of memory management, scrolling performance, etc... ;-)

    0 讨论(0)
提交回复
热议问题