[Android] Chapter 3 (3) LinearLayout (linear layout) and linearlayout
Category: C #, Android, VS2015;
Created on:
I. Introduction
LinearLayout stacks components in the container one by one horizontally or vertically (without overlap ). This layout is very similar to the StackPanel control of WPF. It also sets whether the direction of the layout is vertical or horizontal through the orientation attribute ).
Common attributes include:
Android: layout_gravity: Alignment of child elements in the container. That is, to which side is sunk (gravity: gravity ).
Android: orientation: controls the arrangement of child elements (horizontal and vertical)
Android: layout_weight: The allocation weight of the space occupied by its child elements. The smaller the value, the larger the weight.
For example:
<LinearLayout
Android: orientation = "horizontal"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_weight = "1"
Note: The width and height of sub-elements must have at least one linear layout set to "fill_parent.
Ii. Example-Demo01LinearLayout
1. Design Interface
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android =" http://schemas.android.com/apk/res/android "Android: orientation =" vertical "android: layout_width =" match_parent "android: layout_height =" match_parent "> <LinearLayout android: orientation =" horizontal "android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1"> <TextView android: text = "red" android: gravity = "center_horizontal" android: background = "# aa0000" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> <TextView android: text = "green" android: gravity = "center_horizontal" android: background = "#00aa00" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> <TextView android: text = "blue" android: gravity = "center_horizontal" android: background = "# tianaa" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> <TextView android: text = "yellow" android: gravity = "center_horizontal" android: background = "# aaaa00" android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: layout_weight = "1"/> </LinearLayout> <LinearLayout android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_weight = "1"> <TextView android: text = "Row 1st" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> <TextView android: text = "row 2nd" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> <TextView android: text = "Row 3rd" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> <TextView android: text = "Row 4th" android: textSize = "15pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_weight = "1"/> </LinearLayout>
4. Add the Demo01LinearLayout. cs file.
Add the file in the SrcDemos folder.
using Android.App;using Android.OS;namespace ch07demos.SrcDemos{ [Activity(Label = "Demo01LinearLayout")] public class Demo01LinearLayout : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.Demo01LinearLayout); } }}
Observe the running effect.