前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Monkey Android》第7课RelativeLayout、TableLayout

《Monkey Android》第7课RelativeLayout、TableLayout

作者头像
GitOPEN
发布2019-01-29 10:57:33
5110
发布2019-01-29 10:57:33
举报

通过本节课可以学习到的内容:

  • RelativeLayout以及它的相关属性
  • TableLayout以及它的特有属性

实例代码:

运行效果参见本课程示例App:安卓猴Demos

github地址:https://github.com/git0pen/MonkeyAndroid


RelativeLayout

顾名思义,RelativeLayout就是相对布局,置于其中的控件在摆放的时候需要相对于布局中的其它控件来摆放。

RelativeLayout相关属性

这些属性十分“众多”,因此大致了解即可,关键是在敲代码的过程中熟练运用。

属性

作用

第1组属性

相对属性

android:layout_below

将目标控件置于引用控件的下方

android:layout_above

将目标控件置于引用控件的上方

android:layout_toLeftOf

将目标控件置于引用控件的左方

android:layout_toRightOf

将目标控件置于引用控件的右方

第2组属性

对齐属性

android:layout_alignTop

目标控件和引用控件的上边缘对齐

android:layout_alignBottom

目标控件和引用控件的下边缘对齐

android:layout_alignLeft

目标控件与引用控件的左边缘对齐

android:layout_alignRight

目标控件与引用控件的右边缘对齐

android:layout_alignBaseLine

基于基准线对其,基准线就是我们写英文字母那4行线的第三条

第3组属性

这组属性的值是 true 或者 false

layout_alignParentRight

是否与父控件的右边缘对齐

layout_alignParentLeft

是否与父控件的左边缘对齐

layout_alignParentTop

是否与父控件的上边缘对齐

layout_alignParentBottom

是否与父控件的下边缘对齐

第4组属性

中间属性

layout_centerInParent

与父控件在水平方向和垂直方向都对齐

layout_centerVertical

与父控件在垂直方向都对齐

layout_centerHorizontal

与父控件在水平方向都对齐

第5组属性

引用属性

layout_alignStart

引用其他控件,表示与控件的开始位置对齐

layout_alignStop

引用其他控件,表示与控件的结束位置对齐

layout_alignParentStart

取值为true、false,表示与父控件的开始位置对齐

layout_alignParentStop

取值为true、false,表示与父控件的结束位置对齐

TableLayout

顾名思义,TableLayout布局就是表格布局。其实现的效果就如同上面的属性表格一样。

TableLayout的特有属性

  • android:stretchColumns="1"设置所用行的第二列为扩展列,如果有三列的话,剩余空间由第二列补齐。
  • android:shrinkColumns="1"设置所用行第二列为收缩列。
  • android:layout_column="0"表示当前控件在表格中的第0列,视觉效果上是第1列。
  • android:layout_span="2" 表示当前控件跨了两列。
<?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"
  >

  <TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="#41c2fa"
    android:text="我右边是Button"
    android:textColor="#fc0000"
    android:textSize="22sp"
    />

  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="center_horizontal"
    android:layout_toEndOf="@+id/text_view"
    android:layout_toRightOf="@+id/text_view"
    android:text="我下边是ImageView"
    />

  <ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_below="@+id/button"
    android:src="@mipmap/ic_launcher"
    />

  <TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/image_view"
    android:stretchColumns="1"
    >


    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >

      <TextView
        android:id="@+id/text_view_uname"
        android:layout_column="0"
        android:text="用户名:"
        android:textSize="16sp"

        />

      <EditText
        android:id="@+id/edit_text_uname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        />
    </TableRow>

    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >

      <TextView
        android:id="@+id/text_view_pwd"
        android:layout_column="0"
        android:text="密码:"
        android:textSize="16sp"
        />

      <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:ems="10"
        android:inputType="textPassword"
        />
    </TableRow>


    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >

    </TableRow>


    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >

      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_column="0"
        android:layout_gravity="right"
        android:layout_span="2"
        android:orientation="horizontal"
        >

        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="登陆"
          />

        <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="注册"
          />

      </LinearLayout>

    </TableRow>


  </TableLayout>

</RelativeLayout>

下课

这一节课,我们学习了RelativeLayout和TableLayout的用法,其中前者是必须重点掌握的布局,后者是需要了解的布局;熟练灵活地使用RelativeLayout布局,可以让你在今后的项目开发中对UI的把控更加游刃有余。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/12/06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • RelativeLayout
    • RelativeLayout相关属性
    • TableLayout
    • TableLayout的特有属性
    • 下课
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档