Android初试--LinearLayout(线性布局)

一时失言乱红尘 2022-08-26 01:20 368阅读 0赞

Linearlayout(线性布局)

LinearLayout以给它设置的垂直或水平的属性值,来排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一个垂直列表的每一行只会有一个元素,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子元素的高度加上边框高度)。LinearLayout保持子元素之间的间隔以及互相对齐(相对一个元素的右对齐、中间对齐或者左对齐)。它还支持为单独的子元素指定weight。好处就是允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤成一堆的情况,而是允许他们放大填充空白。子元素指定一个weight值,剩余的空间就会按这些子元素指定的weight比例分配给这些子元素,默认的weight值为0。例如,如果有三个文本框,其中两个指定了weight值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大。fill_parent或者match_parent(Android2.2开始使用match_parent),强制性让一个顶部布局或控件布满整个屏幕。wrap_content即包裹内容,此时布局元素将根据内容更改大小。

下面我们举例说明:

工程的res文件的layout文件夹中的主布局文件:



TextView android:id="@+id/lable" android:layout\_width="match\_parent" android:layout\_height="wrap\_content" android:text="用户名" /

EditText android:id="@+id/input" android:layout\_width="match\_parent" android:layout\_height="wrap\_content" android:text="请输入用户名" /

Button android:id="@+id/button" android:layout\_width="match\_parent" android:layout\_height="wrap\_content" android:text="登录" /

代码说明:

android:orientation=”vertical” 指定线性布局的方向为垂直方向

android:layout_width=”match_parent” 指定组件的宽度为匹配父容器

android:layout_height=”wrap_content” 指定组件的高度为包裹内容

效果如图:

SouthEast

工程的res文件夹中values文件夹中的strings.xml文件中键入:

用户名 请输入 登录

工程的res文件的layout文件夹中的主布局文件:

<?xml version=”1.0” encoding=”utf-8”?>



TextView android:id="@+id/lable" android:layout\_width="50dp" android:layout\_height="wrap\_content" android:text="@string/name" /

EditText android:id="@+id/input" android:inputType="text" android:layout\_width="200dp" android:layout\_height="wrap\_content" android:text="@string/input" /

Button android:id="@+id/butotn" android:layout\_width="70dp" android:layout\_height="wrap\_content" android:text="@string/login" /

代码说明:

android:orientation=”horizontal” 指定线性布局的方向为水平方向

SouthEast 1

用途实例:线性布局是android布局中使用最广的。可以说在任何一种布局文件中都有它的身影。

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读