-1

I am developing for Android and I ran into a problem. When I use addView method of ViewGroup to add a ViewGroup object, it does not display anything on the screen.

Can anyone help me?

RelativeLayout fullLayout=new RelativeLayout(this); 
RelativeLayout top=new RelativeLayout(this); 
TextView phoneNumber=new TextView(context); 
phoneNumber.setText("137928833"); 
RelativeLayout.LayoutParams phoneParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
phoneParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); phoneParams.leftMargin=130; phoneParams.topMargin=120; 
top.addView(phoneNumber,phoneParams); 
fullLayout.addView(top,0); 

I use setContentView(fullLayouot),it does not work.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • please show some code. What are you trying to do and where did you got stuck? – Abdullah Shoaib Aug 18 '13 at 13:27
  • RelativeLayout fullLayout=new RelativeLayout(this); RelativeLayout top=new RelativeLayout(this); TextView phoneNumber=new TextView(context); phoneNumber.setText("137928833"); RelativeLayout.LayoutParams phoneParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); phoneParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); phoneParams.leftMargin=130; phoneParams.topMargin=120; top.addView(phoneNumber,phoneParams); fullLayout.addView(top,0); I use setContentView(fullLayouot),it does not work. – xiaoxiaoniao Aug 18 '13 at 13:54
  • @xiaoxiaoniao: why dont you keep the above code (comment) in your question itself , so that every one can understand your problem clearly – Vamsi Pavan Mahesh Aug 18 '13 at 14:46

2 Answers2

0

It simply doesn't work because you didn't set any layout parameters on your top level layout (fullLayout). no width no height and nothing....

on the other hand, you have instantiated relative layout params with ViewGroup values which normally should through exception.

Masoud Dadashi
  • 1,044
  • 10
  • 11
-1

I'm not too sure what you are trying to do; but instead of programmatically creating these views you could create an xml holding them and inflate that layout. Here are some other questions that were asked that might be useful:

Community
  • 1
  • 1
Ryan Smith
  • 1,628
  • 3
  • 13
  • 18