How can we use a variable in R.id

后端 未结 2 2038
孤街浪徒
孤街浪徒 2021-01-13 06:43

The content of xml are



        
2条回答
  •  没有蜡笔的小新
    2021-01-13 07:08

    the id is an intiger value not string: hope u ll get an idea from below code

     while (ctr<3)
    {
       int layoutid;
        if(ctr==1)
        { layoutid = R.id.AbsoluteLayout1;}
        else{
         layoutid = R.id.AbsoluteLayout2;}
        mainlayout[ctr] = (AbsoluteLayout)findViewById(layoutid);
        ctr++;
    }
    ---------------------------------------------
    

    all these won post an error as they are handled as int itself manipulate as you want you cant use this as it is as

    int[] ctra={R.id.xx,R.id.xxx};
      int i=0;
     while (ctr<3)
    {
    mainlayout[i]=(AbsoluteLayout)findViewById(ctra[i]);
    i++;}
    

提交回复
热议问题