Android Layout learning-Layout (Layout) I
There are two ways to declare layout: 1. Declare the UI component in the xml file. 2. instantiate the layout element at runtime. We can create View or ViewGroup objects by encoding and manipulate their attributes. The following uses a small example to learn how to add layout in encoding: Copy code 1 import android. app. activity; 2 import android. graphics. color; 3 import android. OS. bundle; 4 import android. view. viewGroup; 5 import android. widget. button; 6 import android. widget. linearLayout; 7 import android. widget. textView; 8 9 public class MainActivity extends Activity {10 11 private LinearLayout linearLayout; 12 private TextView textView; 13 private Button bu Tton; 14 public static final int VERTICAL = 1; 15 public static final int MATCH_PARENT =-1; 16 public static final int WRAP_CONTENT =-2; 17 @ Override18 protected void onCreate (Bundle savedInstanceState) {19 super. onCreate (savedInstanceState); 20 21 // Add layout22 23 linearLayout = new LinearLayout (this); 24 linearLayout. setOrientation (VERTICAL); // set the LinearLayout direction. 0 indicates horizontal, and 1 indicates VERTICAL. The default value is horizontal. 25 // set the layout parameter.-1 is MATCH_PARENT and-2 is WRAP_CONTENT26 // ViewGroup. layoutParams (int width, int height) 27 linearLayout. setLayoutParams (new ViewGroup. layoutParams (MATCH_PARENT, MATCH_PARENT); 28 29 textView = new TextView (this); 30 textView. setText ("ThisIsATextView"); 31 textView. setBackgroundColor (Color. RED); 32 textView. setLayoutParams (new ViewGroup. layoutParams (MATCH_PARENT, WRAP_CONTENT); 33 34 button = new Bu Tton (this); 35 button. setText ("ThisIsAButton"); 36 button. setBackgroundColor (Color. GREEN); 37 button. setLayoutParams (new ViewGroup. layoutParams (MATCH_PARENT, WRAP_CONTENT); 38 39 linearLayout. addView (button); 40 linearLayout. addView (textView); 41 // After the layout is written, do not forget to add it to Activity 42 setContentView (linearLayout); 43 44 45} 46} copy the code to run: each layout file must contain a definite root element, which must be a View or ViewGroup object. What are the roles of the View class and ViewGroup class? View: provides basic building areas for user interface components. The View class is the parent class of widgets. widgets is usually used to Create Interactive UI components such as button and TextView. The View class is also the parent class of the ViewGroup class. ViewGroup: it is the parent class of the layout class, and the layout class is the visualization container that saves other views or viewgroups, and can define their layout attributes. The view layer is gradually improved by adding an additional layout object (layout object) or window (widgets) as child elements. The following describes how to use a layout file: Copy code 1 <! -- The root element LinearLayout is the object of the ViewGroup subclass layout --> 2 <LinearLayout xmlns: 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 sub-components to enrich the video layer --> 8 <Button 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content" 11 android: background = "# f00" 12 android: layout_weight = "1" 13 android: text = "ThisIsAButton"/> 14 <TextView 15 android: layout_width = "match_parent" 16 android: layout_height = "wrap_content" 17 android: background = "#0f0" 18 android: text = "ThisIsATextView" 19/> 20 </LinearLayout>