简体   繁体   中英

Android-Change Layout of view dynamically

I have some view in my activity :

for(int i=0;i<My_NewsGroup.size();i++)
    {
        View My_UpdateView=new View(MainActivity.this);
        My_UpdateView =LayoutInflater.from(MainActivity.this).inflate(R.layout.row2, null);
.....
    }

now i want to change layout of some view dynamically !

how can i do this ?

TnX ---------------------------------EDIT-------------------

LinearLayout ll2 = (LinearLayout)findViewById(R.id.parent2);
    for(int i=0;i<My_NewsGroup.size();i++)
    {
        View My_UpdateView=new View(MainActivity.this);
        My_UpdateView = LayoutInflater.from(MainActivity.this).inflate(R.layout.row2, null);

                     ....
                     .....          
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

        ll2.addView(My_UpdateView,relativeParams);

        MyViewList.add(My_UpdateView);
    }

MyViewList is : List MyViewList=new ArrayList();

and then ! for example if i want to hide some View :

MyViewList.get(pos).setVisibility(View.GONE);

but now i want to change layout for some view !

You can't change view itself and reinflate it with different xml layout. You can change view's container content. Remove old view from ViewGroup and replace it with new one. Here is trivial example:

FrameLayout somelayout...
somelayout.removeView(viewYouWantToReplace);
somelayout.addView(inflator.inflate(R.layout.row2, somelayout, false));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM