Layout defines the visual structure of the user interface (visual structure), such as the activity's UI, and the UI of the application window.
There are two ways of declaring layout:
1. declare the UI component in the XML file .
2. at run time, instantiate the layout element . We can create view or ViewGroup objects in a coded fashion, manipulating their properties.
Here's a small example to learn how to add layout in a coded way:
1 Importandroid.app.Activity;2 ImportAndroid.graphics.Color;3 ImportAndroid.os.Bundle;4 ImportAndroid.view.ViewGroup;5 ImportAndroid.widget.Button;6 Importandroid.widget.LinearLayout;7 ImportAndroid.widget.TextView;8 9 Public classMainactivityextendsActivity {Ten One Privatelinearlayout LinearLayout; A PrivateTextView TextView; - Privatebutton button; - Public Static Final intVERTICAL = 1; the Public Static Final intMatch_parent =-1; - Public Static Final intWrap_content =-2; - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - + //add layout in a coded way A atLinearLayout =NewLinearLayout ( This); -Linearlayout.setorientation (VERTICAL);//set LinearLayout direction, 0 is horizontal, 1 is vertical. The default is horizontal. - //Set layout parameters,-1 is match_parent,-2 is wrap_content - //viewgroup.layoutparams (int width, int height) -Linearlayout.setlayoutparams (NewViewgroup.layoutparams (match_parent,match_parent)); - inTextView =NewTextView ( This); -Textview.settext ("Thisisatextview"); to Textview.setbackgroundcolor (color.red); +Textview.setlayoutparams (NewViewgroup.layoutparams (match_parent,wrap_content)); - thebutton =NewButton ( This); *Button.settext ("Thisisabutton"); $ Button.setbackgroundcolor (color.green);Panax NotoginsengButton.setlayoutparams (NewViewgroup.layoutparams (match_parent,wrap_content)); - the Linearlayout.addview (button); + Linearlayout.addview (textView); A //don't forget to add to the activity when the layout is written . the Setcontentview (linearlayout); + - $ } $}
Run:
Each layout file must contain a deterministic root element, which must be a View or viewgroup object.
What is the function of the view class and the ViewGroup class?
View:
Provides a basic building area for user interface components. The view class is the parent of widgets , and widgets is typically used to create interactive UI components
such as Button,textview and so on. The view class is also the parent class of the ViewGroup class.
ViewGroup:
is the parent class of the layout class, and the layout class is a visual container (invisible containers) that holds other view or viewgroup, and can define their placement properties.
gradually refine the view layer by adding additional layout objects (Layout object) or windows (widgets) as child elements.
Here's a layout file to learn the following:
1 <!--determines the root element linearlayout is an object of ViewGroup's subclass layout -2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Wrap_content"6 android:orientation= "vertical" >7 <!--add subcomponents to enrich the view layer -8 <Button9 Android:layout_width= "Match_parent"Ten Android:layout_height= "Wrap_content" One Android:background= "#f00" A Android:layout_weight= "1" - Android:text= "Thisisabutton" /> - <TextView the Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:background= "#0f0" - Android:text= "Thisisatextview" + /> - </LinearLayout>
After we declare the layout of the interface in the XML file, save the res/layout/ XML file below.
I hope this article is helpful to everyone's study, if you like, please recommend, thank you ~
If reproduced, please indicate this blog address at the beginning of the article: Http:www.cnblogs.com/johntsai
Welcome to discuss the Exchange, e-mail: JohnTsai.Work@gmail.com:)
--layout (layout) of the Android layout learning one