在Android中动态更改视图内的视图

3

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

蓝色部分是我想要加载不同视图的地方。 alt text

@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){
    }
}

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

你考虑过使用TabWidget来完成相同的任务吗? - Mudassir
是的,我做了,但是我有一个保存按钮(opslaan),可以保存所有3个视图的信息。而且在顶部或底部放一个大的保存按钮看起来真的很“丑”。 - Galip
2个回答

5
我找到了答案:

我有点找到了答案:

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'部分(图片中的蓝色部分)

我是这样声明'body'的:

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

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

它会显示 java.lang.IndexOutOfBoundsException: index=1 count=0

我该如何修复这个问题?

编辑:已解决:

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

0
  • 使用findViewById获取对每个RadioButton的引用
  • 使用setOnClickListener在每个RadioButton上注册一个OnClickListener
  • OnClickListener回调中,加载不同的背景

我已经有一个方法来确定哪个单选按钮被选择了。唯一的问题是我无法在当前视图中设置不同的XML视图。我已经编辑了我的问题以包含我的方法。 - Galip

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接