What is Activity_main XML in Android
The design of a display is implemented by two things:
- The activity triggers the display of the elements and reacts to the user events. In principle, the activity could also design the display in Java. Usually this is not done, however.
- Instead, the activity unfolds by calling the method setContentView an XML file in the directory res / layout is defined. How a screen is designed with it is the subject of this website.
A fragment also uses an XML file for design. The fragment calls in its handler method onCreateView The method inflate on.
After creating a project, Android Studio creates the program framework for the main activity and the file at the same time res / layout / activity_main.xml.
The editor for the layoutIf you click on this file, it is displayed in three ways, which can be toggled using the symbol buttons at the top right:
- A picture (rectangle with mountains) is the design mode. The picture is divided into
- a palette on the left: it contains many, many control elements (views) that can be dragged onto the display.
- the display view in the middle: Here you can collect elements and combine them with one another.
- The attribute settings on the right: The control elements can be labeled or colored or are given identifiers via which they can be accessed by other elements but also by activity.
- A symbol made up of lines and rectangles stands for the split mode. This allows you to see the XML code and the effects on the display side by side.
- The symbol, in which there are several lines below each other, stands for the XML code editor. Sometimes it is easier, sometimes inevitable, to edit the generated code.
Arrangement in a ViewGroupAn XML file combines one or more control elements that are saved as View are designated. The framework forms a ViewGroupwhich also determines how the children are arranged.
- LinearLayout: All children are arranged next to or on top of each other.
- RelativeLayout: The children can indicate how they want to be positioned next to or below the other children.
- ConstraintLayout: In the current versions, ConstraintLayout is used as the standard. Although this is more complex than the others, it requires less nesting of ViewGroups.
- The ViewGroup uses the entire space (match_parent) of the parent element. In this case it is the frame of the display.
- The TextView, on the other hand, only encloses the content (wrap_content) and leaves the rest of the space to other views that need it.
distancesThe views can create space in two ways:
- margin is the distance to the outside. The view keeps a distance to the edge or to the neighbor: andoid: layout_marginLeft, andoid: layout_marginRight, andoid: layout_marginTop and andoid: layout_marginBottom.
- padding is the distance to the inside, i.e. to its content: andoid: paddingLeft, andoid: paddingRight, andoid: paddingTop and andoid: paddingBottom.
Common units for example for distances are
- dp (Density-independent pixel): This size ensures that the expansion is roughly the same on every device, even if the resolution is higher.
- sp (Depending on text): On very small screens, an edge of one centimeter is a bit unfortunate. That is why sp is preferred in such cases, as the font is smaller on smaller devices.
The units mm, in, pt (point - a unit from the pressure range) are also possible. However, these are not independent of the device hardware and should therefore be avoided.
IDEach element of a layout can be identified by an ID. This is done by adding an android: id. android: id = "@ + id / meinElement" This ID is important if you want to read out or set the control element from the Java program, but also if you want to define the relationship between elements within the layout: app: layout_constraintStart_toEndOf = "@ id / myElement "
LinearLayoutIn a linear layout, the elements are always arranged one below the other or next to one another. The direction is determined by the Orientation attribute. Its value can or be.
RelativeLayoutIn the RelativeLayout, the elements are created in relation to other elements, e.g. under a button or to the left of a TextView.
|android: layout_above||The element is on top of the other element.|
|android: layout_below||The element lies below the other element.|
|android: layout_toLeftOf||The element is to the left of the other element.|
|android: layout_toRightOf||The element is to the right of the other element.|
|android: layout_toStartOf||The element is based on the beginning of the other element. So the elements start in the same vertical position.|
|android: layout_toEndOf||The element aligns with the end of the other element.|
ConstraintLayoutThe ConstraintLayout organizes its elements through constraints that describe the relationship between elements. In this way it is even more flexible than the RelativeLayout.
|app: layout_constraintStart_toStartOf = "parent"|
|app: layout_constraintStart_toEndOf = "@ id / cancel_button"|
|app: layout_constraintTop_toTopOf = "parent"|
|app: layout_constraintTop_toBottomOf = "@ id / recyclerView"|
|app: layout_constraintEnd_toEndOf = "parent"|
|app: layout_constraintBottom_toTopOf = "@ id / ok_button"|
|app: layout_constraintBottom_toBottomOf = "parent"|
ChainsIf several elements are to be evenly distributed in a row, they can be marked and then the Chains | Horizontal Chain item can be selected with the right mouse button.
Nesting layoutsLayouts can be nested. One or more other layouts can be located within the RelativeLayout, such as a linear layout here.
Align buttons next to each other in the middleA horizontal linear layout is created so that horizontal buttons use the space (halfway) evenly. A weight is used to determine which button must be wider than the others. If the weights are all the same, they align themselves evenly.
- How do you describe your perfect soulmate
- Immodium heals stomach pain
- Is recycling of electronics profitable?
- Is GTA V hackable online
- What does an above average BUN mean
- Where can I find Eminem songs
- What are your fees for SEO SMO
- Is Ariana Grande on drugs
- Can a placebo be used for anything
- What is the most luxurious small SUV
- What are the 5 phases of SDLC
- What is the toughest encryption currently available
- What does add
- What's the worst about losing
- Why is Russia jealous of huge Vietnam
- How do you say genius in German?
- How many Brahmins live 1
- Is Gatsby hair wax good
- What is a butt cheek
- How do i go to boston
- What if Lyanna Stark never got pregnant
- How should I behave with a friend
- What are measurement fees from IIIT Bangalore
- What an emery cloth for copper pipes