If the layout_width attribute of any control is set to match_parent, it takes up all the available space of the container, hiding the rest of the controls behind it. We also need to set the layout_width attribute of the three controls to wrap_content otherwise, we will be able to see only the first Button control, the one with the Apple ID. To see the controls appear horizontally, set the orientation attribute of the LinearLayout to horizontal. Three Button controls arranged vertically in LinearLayout We see the output shown in Figure 3.1.įigure 3.1. Finally, the width of the three controls is set to match_parent, so that the width of the three controls expands to fill up the available space of the LinearLayout container. The height of the three controls is set to wrap_content, which is enough to accommodate the text. The IDs and text assigned to the three Button controls are Apple, Mango, and Banana, respectively. Three Button controls are added to the layout, which appear one below the other. The height and width of the layout are set to expand to fill up all the available space of the enclosing container, that is, the device screen. The orientation of LinearLayout is set to vertical, declaring that we want to arrange its child elements vertically, one below the other. The activity_linear_layout_app.xml File on Adding Three Button Controls Modify the activity_linear_layout_app.xml to appear as shown in Listing 3.2. Let’s apply the LinearLayout and add three Button controls to the layout. Default Code in the Layout File activity_linear_layout_app.xml The original default content of the layout file activity_linear_layout_app.xml appears as shown in Listing 3.1. Create a new Android Project called LinearLayoutApp. Let’s see how the controls are laid out in the LinearLayout layout using an example. To set the padding at runtime, we can call the setPadding() method. When assigned to the control’s height or width, this attribute forces the size of the control to expand to fill up all the available space of the enclosing container.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |