Dynamically change view inside view in Android

假装没事ソ 提交于 2019-12-18 05:11:32

问题


I have this RelativeView with 3 radiobuttons on top. I want to change the bottom part of the view when the user clicks on one of the buttons.

The blue part is the place i want to load different Views in.

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    if (checkedId == iVerzekeringen){
        layout.removeView(body);   // THIS PART IS
        layout.addView(testview);  // NOT WORKING
    }
    else if (checkedId == iPersoonlijk){
    }
    else if (checkedId == iNotities){
    }
}

How can i load different Views into the blue part?


回答1:


I kinda found the answer:

layout = (RelativeLayout)findViewById(R.id.rellayout);
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
layout.addView(layoutInflater.inflate(R.layout.gegevens_verzekeringen, group, false), 1 );

This loads the view over the parent xml. I want it to load it over just the 'body' part (the blue part in the picture)

I declared the body like this:

body = (RelativeLayout)findViewById(R.id.body);

But when i do body.addView(layoutInflater.inflate(R.layout.gegevens_verzekeringen, group, false), 1);

It sais java.lang.IndexOutOfBoundsException: index=1 count=0

How can i fix this?

Edit: Got it:

body.addView(layoutInflater.inflate(R.layout.gegevens_verzekeringen, null));



回答2:


  • use findViewById to get a reference to each RadioButton
  • Register an OnClickListener using setOnClickListener on each RadioButton
  • Inside your OnClickListener callback, load a different background


来源:https://stackoverflow.com/questions/4427278/dynamically-change-view-inside-view-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!