LinearLayout: Linear Layout
In general, when many controls need to be listed in an interface, we can use LinearLayout. LinearLayout is to sort the child elements in order in vertical or horizontal . Each child element is located after the previous element. Let's briefly understand it below.
In XML layout files, you will encounter the following units
px: is the pixel point of the screen dp: an abstract unit based on density. The physical size of the screen sp: similar to dp, but will be scaled according to the user's font size
The XML code is as follows: Change the vertical direction android:orientation="vertical" ( vertical )
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.adapter.MainActivity"> <TextView android:text="first TextView" android:background="#ef0808" android:gravity="center" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="100dp" /> <TextView android:text="second TextView" android:gravity="center" android:background="#31ef0b" android:textSize="18sp" android:layout_width="match_parent" android:layout_height="100dp" /> <TextView android:text="third TextView" android:gravity="center" android:textSize="18sp" android:background="#ec07ca" android:layout_width="match_parent" android:layout_height="100dp" /> <TextView android:text="4th TextView" android:gravity="center" android:textSize="18sp" android:background="#f5d105" android:layout_width="match_parent" android:layout_height="100dp" /></LinearLayout>
Running result: Each TextView is arranged from top to bottom.
The XML code is as follows: Change the horizontal direction android:orientation="horizontal" ( horizontal )
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.administrator.adapter.MainActivity"> <TextView android:text="first TextView" android:background="#ef0808" android:gravity="center" android:textSize="18sp" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:text="second TextView" android:gravity="center" android:background="#31ef0b" android:textSize="18sp" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:text="third TextView" android:gravity="center" android:textSize="18sp" android:background="#ec07ca" android:layout_width="100dp" android:layout_height="100dp" /> <TextView android:text="4th TextView" android:gravity="center" android:textSize="18sp" android:background="#f5d105" android:layout_width="100dp" android:layout_height="100dp" /></LinearLayout>
Running result: Each TextView is arranged horizontally from left to right.
The only difference between these two linear layouts is the difference in the value of android:orientation
Experiment summary: The only difference between these two linear layouts is that the values of android:orientation are different. Through this experiment, we have a preliminary understanding of the linear layout in Android.
The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!