Pergunta

I have a layout xml file with a linear layout. One of the children is again a ViewGroup Relative layout. In my java code i want to change the width of this child Viewgroup for my requirements. I tried this

enter code here

    ViewGroup childViewGroup = (LinearLayout)findViewById(childViewGroup);
    LayoutParams l = childViewGroup.getLayoutParams();
    l.width = 360; 
    childViewGroup .setLayoutParams(l);

I couldn't do this because findViewById(childViewGroup) doesn't fetch ViewGroups it does only for Views.

Note: I cant define a whole new layout.xml for this minor requirement since it is huge layout file and might cause performance overhead. I wanted to just change the width of the child view group in my java activity code.

Thanks in advance for your help.

Foi útil?

Solução

ViewGroup is a subclass of View so findViewById should work fine. You're casting the result to LinearLayout, is the childViewGroup a LinearLayout or RelativeLayout as you specified in the question? – codelark Oct 6 at 23:21

Outras dicas

You may use ViewStubs.

You can find a reference here:
http://developer.android.com/resources/articles/layout-tricks-stubs.html

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top