Linear Layout

来源:互联网 发布:java获取彩票开奖数据 编辑:程序博客网 时间:2024/04/23 22:15

LinearLayout


LinearLayout又称作线性布局,在这种布局里的各组件会按单一的方向(水平或竖直)排列,我们可以在XML里通过设置android:orientation属性来设置children的排列方向。

正如它的名字一样,这个布局里所包含的空间会在线性方向上依次排列,下面实际操作尝试一下

XML代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button1"        android:textSize="50sp"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button2"        android:textSize="50sp" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button3_1"        android:textSize="50sp"/></LinearLayout>

效果图:

这里写图片描述


现在我们将orientation的值换成horizontal,效果图如下:

这里写图片描述


小结:当LinearLayout排列方向horizontal时,不管有多少组件,都会在在同一行,于是内部的控件就绝对不能将宽度指定为match_parent,因为这样的话单独一个控件就会将整个水平方向占满,其他控件就没有栖息之所了。
同理,当LinearLayout排列方向vertical时,不管有多少组件,每一行都只能有一个,于是内部的控件就绝对不能将高度指定为match_parent。


接下来总结几个关键属性的用法

  • android:layout_gravity:用于指定控件在布局中的对齐方式,可选值有top,bottom,left,right,center,center_horizontal等等(详情点击)。需要注意的是,当LinearLayout的排列方向是horizontal时,只有垂直方向上的对齐才会发生改变,因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会发生改变,因而无法指定该方向上的对齐方式。同理,当排列方向是vertical时,值有水平方向上的对齐方式才会生效。

说了一大堆,写个代码看看

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button1"        android:textSize="50sp"        android:layout_gravity="top" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button2"        android:textSize="50sp"        android:layout_gravity="center_vertical"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button3_1"        android:textSize="50sp"        android:layout_gravity="bottom"/></LinearLayout>

效果图如下:

这里写图片描述

此LinearLayout的排列方向是horizontal,因此我们只能指定垂直方向上的排列方向。


  • android:layout_weight:使用比例的方式来控制空间大小。我们的程序可能运行在不同的设备上,这就要求我们对屏幕的适配性做出考虑,使用比例的方式就可以比较好的解决这个问题。
  • android:layout_hint:这个属性是用来给编辑框设置提示性文字。

    下面来实际操作一下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal">    <EditText        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:hint="@string/type"/>    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:text="@string/send_bn"/></LinearLayout>

效果图:

这里写图片描述

由代码可以看出,将layout_width都设为0,然后将两个组件的layout_weight都设为1,代表这两个组件将在水平方向上平分宽度。


这似乎和我们平时看到的不太一样,按钮没有必要做这么宽,这时我们可以将按钮的layout_width设为wrap_content,删除layout_weight而将EditText的layout_weight设为1,width继续为0dp。
这样代表着Button的宽度取决于其中内容的宽度而水平方向上剩下的屏幕将由EditText占满。

LinearLayout差不多就是这样了~


知识点参考自Android官方文档及《第一行代码》/郭霖

0 0
原创粉丝点击