您的位置:首页 > 移动开发 > Android开发

Android开发从入门到放弃(5)使用LinearLayout

2016-12-24 16:22 260 查看
本篇博客简单介绍下Android开发中LinearLayout的用法。 LinearLayout是典型的流式布局,垂直布局或水平布局。LinearLayout使用起来比较简单,只有这么几个属性需要注意

orientation

orientation属性是LinearLayout是最基础属性,决定了子元素是垂直流式布局还是水平流式布局。可以设置为vertical和horizontal其中一个值。

填充方法

填充方法有三个可以为三种值:

wrap_content 元素的大小由自身的内容大小来决定,也就是大小刚刚够用

match_parent 元素的大小由父容器的大小决定,能多大就多大

具体的值,如10dp。

layout_weight

如果一个LinearLayout中有两个或多个元素的填充方法是match_parent的话,那么,具体的大小该如何分配呢?答案就是由layout_weight属性来决定。如果有三个控件的height属性是match_parent,并且layout_weight值分别是1,2,3的话,那么他们的高度将分别是可用空间的1/6,1/3和1/2,layout_weight的值越大,分到的空间就越大。

layout_gravity

在一个oritation = verticle的LinearLayout中,默认的水平方向是自左向右,在oritation = horizontal的LinearLayout中,默认的垂直方向是自上到下。但是这个是可以通过layout_gravity属性来控制的。

在一个oritation = verticle的LinearLayout中,默认的水平方向是自左向右,但可以将layout_gravity设置为left(默认), center_ horizontal, and right。水平布局的LinearLayout中,可以将layout_gravity设置为top(默认),center_vertical和bottom。

还有一个属性叫gravity,这个属性是决定元素自身内容的延伸方向的。如果将一个文本框的gravity设置为right,则该文本框的文字将居右侧显示。

使用LinearLayout的例子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zdk.relativelayoutdemo.LinearLayoutActivity"
android:orientation="vertical"
>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="TextView1"
/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="TextView2"
android:layout_weight="1"
android:gravity="top"
android:layout_gravity="right"
android:layout_margin="10dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="TextView3"
android:layout_weight="2"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局
相关文章推荐