Help with Android LinearLayout or RelativeLayout

I need to create two views programmatically (because I need to access the ondraw of one of the views). For some reason, no matter what I do, to add views to the contentview, they don't appear vertically, one below the other.

I can do this very well using XML, using RelativeLayout and layout layout, but with XML I cannot create a view object and overload the ondraw method.

What am I doing wrong with the software approach and how to solve this problem?

LinearLayout mLinearLayout; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create a LinearLayout in which to add the ImageView mLinearLayout = new LinearLayout(this); TextView tv = new TextView(this); tv.setBackgroundColor(0xff333333); tv.setText("Enter your member number:"); tv.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); DrawableView i = new DrawableView(this); i.layout(0,40,0,0); i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mLinearLayout.addView(tv); mLinearLayout.addView(i,300,300); setContentView(mLinearLayout); } 
+2
source share
3 answers

you can set the orientation of the line layout object to vertical

+1
source

There seem to be two problems, although it is harder to debug this code than XML.

At first, you were not able to set the vertical orientation for the linear layout. For horizontal orientation, everything will be one line. For vertical layout, each widget will be on a separate line, going from top to bottom.

Secondly, "Fill Parent" looks suspicious. Any filling parent element, unless it allows for relative layout or absolute size, takes care of everything and hides all other widgets. This is almost always the wrong decision. Instead, try using weight.

At first, you can think about exactly how you want to use XML, and then convert it to a program setting, while keeping the conversion as mechanical as possible. Good luck

+1
source

You tried to set the layout orientation to verticcal:

mLinearLayout.setOrientation(1);

+1
source

All Articles