布局的概念

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

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。
运行结果如下:

RelativeLayout

相对布局,是比较推荐使用的布局。下面给出各种属性(中文和英文两个版本)



采用官方的例子说明部分内容,具体代码如下:

<?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还不会,,,):

说明几个我认为难理解的:
android:layout_alignParentRight=”true”,应该是对齐父视图(这里应该是布局)的右边。其它的属性见名知意吧(align对齐的意思)。

FrameLayout

在碎片中再介绍

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

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

更多相关文章

  1. 第一行代码 第三章 RecyclerView
  2. Android(安卓)4.0.3 联系人(通讯录)应用源码学习
  3. Android——ViewPager和Fragment混合使用(含源码下载)
  4. Andriod应用开发--第二章作业1--习题答案
  5. Android(安卓)Selector和Shape的用法
  6. [置顶] Android屏幕适配全攻略(最权威的官方适配指导)
  7. Android之ConstraintLayout
  8. Android(安卓)Studio--android:descendantFocusability用法简析
  9. Android横向滑动加载更多的控件的实现---HorizontalScrollSlideV

随机推荐

  1. 【Android(安卓)界面效果4】android背景
  2. Android面试-Android项目构建
  3. Android(安卓)项目的创建及工程目录的介
  4. AndroidMainifest标签使用说明1——<acti
  5. Android中的surfaceHolder.lockCanvas(nu
  6. 浅入浅出Android(010):如何将已有的sqlite数
  7. TabWidget
  8. Android(安卓)BlueDroid(三):BlueDroid蓝牙
  9. Android(安卓)签名打包出现的错误的解决
  10. Android(安卓)Studio 3.3配置Butterknife