Complete the code to create a vertical LinearLayout container.
val layout = LinearLayout(this).apply {
orientation = [1]
}The orientation property sets the layout direction. Use LinearLayout.VERTICAL for vertical stacking.
Complete the code to add a TextView inside the Box layout with match_parent width.
val textView = TextView(this).apply {
layoutParams = LinearLayout.LayoutParams([1], LinearLayout.LayoutParams.WRAP_CONTENT)
text = "Hello Box"
}Use MATCH_PARENT to make the TextView fill the parent's width.
Fix the error in setting the background color of the Box layout.
layout.setBackgroundColor([1].parseColor("#FF0000"))
The Color class provides the parseColor method to convert color strings.
Fill both blanks to create a Box layout with padding and gravity centered.
layout.apply {
setPadding([1], [1], [1], [1])
gravity = [2]
}Padding of 16 pixels is common for spacing. Gravity.CENTER centers children inside the layout.
Fill all three blanks to create a Box layout with horizontal orientation, weight sum, and a weighted child.
val layout = LinearLayout(this).apply {
orientation = [1]
weightSum = [2]f
}
val child = View(this).apply {
layoutParams = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, [3]f)
}Set orientation to HORIZONTAL for side-by-side layout. WeightSum defines total weight. Child weight 0.5 means half the space.