展会信息港展会大全

Android 程式开发:(五)屏幕组件 —— 5.2 LinearLayout线性布局
来源:互联网   发布日期:2015-10-14 07:44:59   浏览:1649次  

导读:LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。想知道LinearLayout是如何工作的,首先考虑一下典型的mail.xml文件。html"><...

LinearLayout把视图组织成一行或一列。子视图能被安排成垂直的或水平的。想知道LinearLayout是如何工作的,首先考虑一下典型的mail.xml文件。

html">

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

</LinearLayout>

在main.xnl中,可以看到,根节点元素是<LinearLayout>,它包含了一个<TextView>元素。这个<LinearLayout>元素控制了它所包含的视图views的顺序。

每一个View和ViewGroup有一些共同的属性,一些例子:

属性

描述

layout_width

指定View或ViewGroup的宽度

layout_height

指定View或ViewGroup的高度

layout_marginTop

指定View或ViewGroup上方的额外空间

layout_marginBottom

指定View或ViewGroup下方的额外空间

layout_marginLeft

指定View或ViewGroup左侧的额外空间

layout_marginRight

指定View或ViewGroup右侧的额外空间

layout_gravity

指定View或ViewGroup中的子视图的排列位置

layout_weight

指定指派给View或ViewGroup的额外空间尺寸

layout_x

指定View或ViewGroup的x坐标

layout_y

指定View或ViewGroup的y坐标

举个例子,<TextView>元素使用了fill_parent常量,所以的宽度填充了整个它所在布局的宽度。它的高度被设置成wrap_content常量,这意味着,它的高度将和它所在的布局一样。如果不想<TextView>占据整个屏幕,可以把layoutt_width元素设置成wrap_content,就像这样:

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello" /><span style="font-family:Arial,Helvetica,sans-serif;">

</span> www.2cto.com

以上的代码,把TextView的宽度设置成和它所包含的文本的长度一样。

请看如下的布局:

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:layout_width="100dp"

android:layout_height="wrap_content"

android:text="@string/hello" />

<Button

android:layout_width="160dp"

android:layout_height="wrap_content"

android:text="Button" />

</LinearLayout>

这里,就把TextView和Button的宽度设置成了一个固定的值

摘自 manoel的专栏

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港