How can dynamically add a View to a ViewGroup and vertically align with one of its child

后端 未结 2 1271
逝去的感伤
逝去的感伤 2021-01-07 02:59

I have a ViewGroup and it has a few children. And one of them is a TextView (\"+id/text\"). In my code, I would like to know how can I add a new View or ViewGroup which will

相关标签:
2条回答
  • 2021-01-07 03:45

    If your layout is not so complex, use RelativeLayout

    A Layout where the positions of the children can be described in relation to each other or to the parent...

    When you insert a new child view, create a new LayoutParams then setRule

    You can put the child view below the textView with this

    :)

    0 讨论(0)
  • 2021-01-07 03:55

    Consider using a TableLayout as your ViewGroup. With a TableLayout you can programmatically add a child at a specific location.

    View view = new ViewIWantToAdd();
    TableLayout layout = (TableLayout)findViewById(R.id.table_layout);
    layout.addView(view);
    

    Would add a view at the bottom of the TableLayout. Typically you would use a TableRow as the contents of the TableView, but you can add any view.

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