android基本布局

布局的概念

布局是一种可用于放置空间的容器,它能够按照一定的规律控制内部控件的位置,从而编出精美的界面,提高用户体验。下面分别介绍常用的几种控件。

LinearLayout

LinearLayout,线性布局,下面先介绍一下LinearLayout中比较重要的属性,然后再举例说明:

  1. 属性android:orientation
    设置布局排列方式,包括:vertical(垂直方向排列)和horizontal(水平方向)。
  2. 属性android:layout_width
    设置宽度,可以用三个常量:fill_parent(API 8之前,现在不提倡),match_parent,wrap_content,还可以使用数字加单位dp。
    注意:当排列方式为horizontal时,宽度不能设置成match_parent或fill_parent。如果设置了,则一个控件就将整个水平方向占满了。
  3. 属性android:layout_height
    设置高度,可以用三个常量:fill_parent(API 8之前,现在不提倡),match_parent,wrap_content,还可以使用数字加单位dp(专门设置布局或控件单位)。
    注意:当排列方式为vertical时,高度不能设置成match_parent或fill_parent,原因和设置宽度一样。
  4. 属性android:layout_weight
    这是一个比较奇葩的属性。它是在控件中设置,表示该控件占布局的权重值。举个例子:线性布局中共四个控件,按垂直方向排列。若其中两个只设置高度成wrap_content,另外两个一个权重是1,一个权重是2,则整个线性布局垂直方向在除掉前两个设置高度的控件所占有的部分外,其余部分分成1+2=3份,权重为1的占1份,权重为2的占两份,这样把整个垂直方向分配完。
    注意:若一个控件设置了权重(非0),则一般宽(若布局是水平方向)或高(若布局是垂直方向)要设置成0dp。
  5. 属性android:layout_gravity
    在控件中设置该控件在布局中的对齐方式,常用的有以下取值:

    • right
    • left
    • center
    • bottom
    • top

    但是该属性受其它控件的影响。当线性布局的排列方式是水平的时,只有垂直方向上的对齐方式才会生效,原因在于水平方向的长度受控件的影响,若再添加控件,则水平方向长度会变化。同样的道理,排列方向为垂直时,只有水平方向的对齐方式才有效。

  6. 设置边距属性,指设置布局在整个界面中的边距包括:
    • android:paddingLeft
    • android:paddingRight
    • android:paddingBottom
    • android:paddingTop
    • android:paddingEnd
    • android:paddingStart

以上属性例子参考代码如下:

<?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">
    <TextView
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="输入信息"/>
    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入用户名"/>
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="输入密码"/>
    <Button
        android:id="@+id/confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="确定"
        />
</LinearLayout>

其中,根元素要有命名空间,xmlns:android。
运行结果如下:
LinearLayout

RelativeLayout

相对布局,是比较推荐使用的布局。下面给出各种属性(中文和英文两个版本)
relativeLayout属性1
relativeLayout属性2
relativeLayout属性3
采用官方的例子说明部分内容,具体代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" />
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="done" />
</RelativeLayout>

运行结果如下(目前Spinner还不会,,,):
RelativeLayout
说明几个我认为难理解的:
android:layout_alignParentRight=”true”,应该是对齐父视图(这里应该是布局)的右边。其它的属性见名知意吧(align对齐的意思)。

FrameLayout

在碎片中再介绍

其它布局就先不介绍了,,,

最后注意一点:布局嵌套太多影响性能,因此最好用相对布局。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值