线性布局
LinearLayout 是一种视图组,负责在其中水平或垂直地保存视图。它是一种布局,可以水平或垂直排列组。
示例图:
句法:
XML
XML
XML
XML
相对布局
RelativeLayout 是一种布局,我们可以在其中根据其他视图/小部件的位置来排列视图/小部件。它独立于水平和垂直视图,我们可以根据一次满意来安排它。
示例图:
句法:
XML
线性布局和相对布局的区别
LinearLayout |
RelativeLayout |
---|---|
We can adjust views and widgets linearly i.e. Horizontally and vertically. | We can adjust views and widgets according to one’s satisfaction. |
layout_weight attribute in the linear layout is used to specify the equal or specific size to the particular widget and view by using the following attribute. android:layout_weight = ‘0’ Here Weight is specified as 0 in order to give equal size or space to each view or widget. |
Various attributes like: layout_toRightOf, layout_toLeftOf, layout_below, layout_alignParentTop, layout_top, layout_alignParentLeft, layout_alignParentRight are used to specify the position of each view and widget. |
It is useful when we arrange views in a linear fashion | It is useful when we arrange views in a relative fashion. |
Syntax: |
Syntax: |
Example: In various Apps, LinearLayout is mainly applicable in the SignUp screen where Name, Email, Phone Number, Submit, etc. are arranged in a linear fashion. | Example: In Google Play Store, when we open the app, the games, books, movies, and App’s sections all are arranges in Relative Layout Fashion. |
LinearLayout is less used as compared to RelativeLayout. | RelativeLayout is used more in applications. |
We can use LinearLayout inside RelativeLayout. | We can also use RelativeLayout as a Child of LinearLayout. |
程序解释 LinearLayout
XML
输出:
程序解释RelativeLayout
XML
输出:
想要一个更快节奏和更具竞争力的环境来学习 Android 的基础知识吗?
单击此处前往由我们的专家精心策划的指南,旨在让您立即做好行业准备!