LinearLayout布局(线性布局),有方向的线性布局;
重要属性:android:orientation,值:horizontal(水平方向),vertical(垂直方向)
LinearLayout相当于html中的div(盒子),所以LinearLayout里也可以放入多个LinearLayout
代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><!--布局方向 “垂直”-->
</LinearLayout>
这里面:
android:layout_width="match_parent"指的是当前的线性布局宽度占整个父元素,这里相对于当前的线性布局父元素为当前的窗体,所以宽度占满窗体
android:layout_height="match_parent"指的是当前的线性布局高度占整个父元素,这里相对于当前的线性布局父元素为当前的窗体,所以高度占满窗体
也可以自定义,如“200dp”:
android:layout_width=“200dp”
android:layout_height=“200dp”
‘dp’作为这里的像素单位。
设置宽度和高度的值除了自定义、“match_parent(占满)”还有“wrap_content(随内容而定)”
代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><!--布局方向 “垂直”-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#cccccc">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffffff">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#f62d59">
</LinearLayout>
</LinearLayout>
效果图:
下面是LinearLayout布局(线性布局)中水平方向的布局样式,
android:orientation="horizontal"
代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><!--布局方向 “垂直”-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#cccccc"
android:orientation="horizontal"><!--布局方向 "水平"-->
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#000000">
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#BDB76B">
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#FFFACD">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffffff">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#f62d59">
</LinearLayout>
</LinearLayout>
效果图:
两者线性布局在实践项目中都尤其重要,比如简单的登录页面:
在大体的线性布局样式确定的情况下,加入一些文本标签(TextView)、按钮标签(Button)、文本输入框(EditText)等等添加属性修饰即可。