How to use getResource.getIdentifier() to get Layout?

前端 未结 1 1632
自闭症患者
自闭症患者 2020-12-01 11:12

I have a question,everyone.

I want to use getResource.getIdentifier() to get my layout element in XML (layout/xxx.xml).

But I am encounter \" R

相关标签:
1条回答
  • 2020-12-01 11:30

    With this:

    int layoutID = getResources().getIdentifier("layout"+n, "layout", getPackageName());
    

    you basically retrieve the id of a layout file that you can inflate. It's the dynamic version of

    int layoutID = R.layout.layout1;
    

    What you intend to do is retrieve a view from an already inflated layout. That's how you'd do it:

    int layoutID = getResources().getIdentifier("layout"+n, "id", getPackageName());
    return (LinearLayout)findViewById(layoutID);
    

    That's the dynamic version of

    return (LinearLayout)findViewById(R.id.layout1);
    
    0 讨论(0)
提交回复
热议问题