How do I to findViewByTag?

前端 未结 1 704

Say I have dynamically generated some amount of LinearLayout, all with different tags.

for (int i = 0; i <= 5; i++)
{
        final LinearLayout LinLayBtn         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 05:32

    Have you tried it this way and found a problem with it?

    You'll need to make this line

    LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("1");
    

    match the naming scheme you used when you set the tag. So you'd want something like this in your example:

    LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("id1");
    

    If you need to do many of these lookups though it would probably be a better approach to store the view references in an array while you are creating them so you don't have to have all of the findView calls. Or do like @Muhammad suggested and use parent.getChild(index i);

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