当前位置: 移动技术网 > IT编程>移动开发>Android > Android布局(RelativeLayout、TableLayout等)使用方法

Android布局(RelativeLayout、TableLayout等)使用方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

河南越调,女性门户,特鲁瓦达

 本文介绍 android 界面开发中最基本的四种布局linearlayout、relativelayout、framelayout、tablelayout 的使用方法及这四种布局中常用的属性。

  • linearlayout 线性布局,布局中空间呈线性排列
  • relativelayout 相对布局,通过相对定位的方式,控制控件位置
  • framelayout 帧布局,最简单的布局,所有控件放置左上角
  • tablelayout 表格布局,以行列方式控制控件位置

四种布局示例

1.linearlayout

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
 
  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">
 
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="垂直1" />
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="垂直2" />
  </linearlayout>
 
  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">
 
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="水平1" />
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="水平2" />
  </linearlayout>
 
  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="horizontal">
 
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="top"
      android:text="水平上对齐" />
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:text="水平垂直居中" />
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom"
      android:text="水平下对齐" />
  </linearlayout>
 
  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">
    <edittext
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="3"
      android:hint="请输入..."/>
    <button
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="2"
      android:text="提交" />
  </linearlayout>
 
  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">
    <edittext
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:hint="请输入..."/>
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="提交" />
  </linearlayout>
</linearlayout>

orientation:horizontal(水平)/vertical(垂直),表示线性排列的方向。
layout_width/layout_height:元素的宽度与高度
layout_gravity:top/bottom/center/left/right/etc,表示当前元素相对父元素的对齐方式,多种对齐方式用“|”隔开,右上对齐:top|right。
layout_weight:占据空间的比例,例如元素a和b,a设置为1,b设置为3, 元素a、b分别占空间的1/4、3/4,此时元素宽度不由layout_width决定,设置为0dp是比较规范的写法。
layout_weight 若元素a设置为1,元素b不设置,将layout_width设置为具体的值或wrap_content,那么元素b的宽度由layout_width决定,元素a将占满屏幕剩下的空间。
2.relativelayout

<linearlayout ...>
  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp">
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignparentleft="true"
      android:layout_alignparentbottom="true"
      android:text="我在左下"/>
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerinparent="true"
      android:text="我在中间"/>
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignparentright="true"
      android:layout_alignparenttop="true"
      android:text="我在右上"/>
  </relativelayout>
 
  <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp">
    <button
      android:id="@+id/button_2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerinparent="true"
      android:text="参照按钮"/>
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@id/button_2"
      android:layout_torightof="@id/button_2"
      android:text="我在右上"/>
    <button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@id/button_2"
      android:layout_toleftof="@id/button_2"
      android:text="我在左下"/>
  </relativelayout>
</linearlayout>

以下属性值为true/false

layout_centerhorizontal/layout_centervertical: 水平居中、垂直居中
layout_centerinparent: 相对父元素垂直&水平居中
layout_alignparentbottom: 元素下边界和父元素下边界对齐
layout_alignparentleft: 左边界对齐
layout_alignparentright: 右边界对齐
layout_alignparenttop: 上边界对齐
以下属性值为控件id

layout_above/layout_below: 在某元素的上方/下方
layout_toleftof/layout_torightof: 在某元素的左方/右方
layout_aligntop/layout_alignbottom: 元素上(下)边界与某元素上(下)边界对齐
layout_alignleft/layout_alignright: 左(右)边界对齐
3.framelayout

所有元素都放置在布局的左上角

<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 
  <button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是一个按钮"/>
  <textview
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是一个输入框"/>
</framelayout>

4.tablelayout

<tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 
  <tablerow>
    <textview
      android:layout_height="wrap_content"
      android:text="邮箱"/>
    <edittext
      android:layout_height="wrap_content"
      android:inputtype="textemailaddress"
      android:hint="请输入您的邮箱" />
  </tablerow>
 
  <tablerow>
    <textview
      android:layout_height="wrap_content"
      android:text="密码"/>
    <edittext
      android:layout_height="wrap_content"
      android:inputtype="textpassword"
      android:hint="请输入密码" />
  </tablerow>
   
  <tablerow>
    <button
      android:layout_height="wrap_content"
      android:layout_span="2"
      android:text="注册" />
  </tablerow>
</tablelayout>

<tablelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:stretchcolumns="1">
  ...
</tablelayout>

tablerow: 代表表格布局的一行,行内一个元素代表一列。
layout_span: 合并单元格,设置为2,代表该元素占据2列空间。
stretchcolumns: tablerow中无法指定空间宽度,那么需要用到该属性,设置为1,表示拉伸第2列(0为第1列)与屏幕一样宽,效果如tablelayout的第二张图。
5.自定义布局

    android中,布局下可以放置控件,也可以放置子布局。如果子布局内容较为独立且经常使用,例如标题栏,或者布局比较复杂,这时候可以考虑使用自定义布局的形式导入。方法很简单。

新建一个布局文件,例如example.xml
在父布局中引入:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="match_parent"
  android:layout_height="match_parent">
   
  <include layout="@layout/example"/>
 
</linearlayout>

以上就是android最基本的四种布局的详细内容介绍,希望对大家的学习有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网