I have this code in my application:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
and I just want to set the orientation of the LinearLayout to vertical. The equivalent in XML is:
android:orientation="vertical"
How can I do it in the code, without XML?
The orientation attribute is used to arrange its children either in horizontal or vertical order. The valid values for this attribute are horizontal and vertical. If the value of the android:orientation attribute is set to vertical, the children in the linear layout are arranged in a column layout, one below the other.
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
Android LinearLayout is a view group that aligns all children in either vertically or horizontally.
Answer: Per documentation, android:weightSum defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly. Let's consider an example with a LinearLayout with horizontal orientation and 3 ImageViews inside it.
You can't change LinearLayout
's orientation using its LayoutParams
. It can be done only with a LinearLayout
object.
LinearLayout layout = /* ... */; layout.setOrientation(LinearLayout.VERTICAL);
You can use like this one:
LinearLayout myll = (LinearLayout) findViewById(R.id.yourLinearLayout); myll.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT)); myll.setOrientation(LinearLayout.VERTICAL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With