0

所以我在运行时创建 ImageView 和 TextView,我希望将 TextView 放在 ImageView 的右侧。

我正在尝试这个简单的代码:

private void loadContent(Module module) {
    this.removeAllViewsInLayout();      
    this.addView(getModuleIconView(module));
    this.addView(getModuleNameView(module));
}

private ImageView getModuleIconView(Module module) {
    ImageView view = new ImageView(context);
    view.setImageResource(module.getIconResId());   
    android.view.ViewGroup.LayoutParams params = new LayoutParams(120, 120);                
    view.setLayoutParams(params);
    view.setId(1); //seting some id for the view
    return view;
}

private TextView getModuleNameView(Module module) {
    TextView view = new TextView(context);
    view.setText(module.getName()); 
    params.addRule(RelativeLayout.RIGHT_OF, 1); //hoping for view to be placed right of the ImageView which id was set to 1
    return view;
}

我在这里做错了什么?TextView 放置在我的 ImageView 上,忽略 RIGHT_OF

需要提一下类继承自RelativeLayout

4

1 回答 1

0

这是我愚蠢的错误。第三种方法泄漏 setLayoutParams

所以正确的方法体是:

private TextView getModuleNameView(Module module) {
    TextView view = new TextView(context);
    view.setText(module.getName()); 
    params.addRule(RelativeLayout.RIGHT_OF, 1);
    view.setLayoutParams(params);
    return view;
}
于 2012-10-26T16:04:40.957 回答