I can't add child views to a ViewGroup that I added programmatically.
First I create a GroupView (_view
) and add it to the Background RelativeLayout (_root
) like this...
_view = new ViewGroup(MainActivity.this) {
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
};
_view.setBackgroundColor(0xFF00FF00);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200);
params.setMargins(10, 0, 10, 0);
_view.setLayoutParams(params);
_view.setTranslationX(0);
_view.setTranslationY(positionY - 80);
selectView(_view);
_root.addView(_view);
_view.setTag(R.string.viewID, "" + viewNum);
_view.setTag(R.string.viewSelected, "" + 0);
viewNum ++;
That works fine. Then I try to add a view to that new GroupView (_view
) like this...
ImageView dragIcon = new ImageView(MainActivity.this);
dragIcon.setImageResource(R.drawable.drabicon);
RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
imgParams.setMargins(0,0,30,0);
dragIcon.setLayoutParams(imgParams);
dragIcon.setTranslationY(_view.getTranslationY());
_view.addView(dragIcon); // This Is What I Want, But This Doesn't Work.
//_root.addView(dragIcon); // Don't Want This, But This Works.
But it doesn't get added, or it isn't visible or something. Now if i use the Background RelativeLayout (_root
) like this _root.addView(dragIcon);
It works fine, the ImageView gets added.
So why doesn't work with the _view
ViewGroup, _view.addView(dragIcon)
?