在Android中动态更改视图内部的视图 [英] Dynamically change view inside view in Android

查看:114
本文介绍了在Android中动态更改视图内部的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个RelativeView,顶部有3个单选按钮。当用户点击其中一个按钮时,我想更改视图的底部。



蓝色部分是我要加载不同视图的位置。

  @Override 
public void onCheckedChanged(RadioGroup group,int checkedId){
if(checkedId == iVerzekeringen){
layout.removeView(body); //这个部分是
layout.addView(testview); // NOT WORKING
}
else if(checkedId == iPersoonlijk){
}
else if(checkedId == iNotities){
}
}

如何将不同的视图加载到蓝色部分?

解决方案

我找到答案:

  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);

这将加载父xml的视图。我想要将其加载到身体部分(图片中的蓝色部分)



我宣布身体如下:

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

但是当我做 body.addView(layoutInflater.inflate(R.layout .gegevens_verzekeringen,group,false),1);



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



如何解决这个问题?



编辑: :

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


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?

解决方案

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));

这篇关于在Android中动态更改视图内部的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆