详解四种基本布局 (layout)

本文详细介绍了Android应用中常见的三种布局方式:线性布局、相对布局和表格布局。通过示例展示了如何使用这些布局来实现不同组件的排列与布局,包括组件的权重分配、比例控制以及相对定位。此外,还阐述了表格布局在输入表单设计中的优势,以及如何通过设置属性来实现表格布局的灵活应用。

1.LinearLayout //线性布局

android:orientation=""  //设置控件的排列方式 horizontal 垂直  vertical 水平
 
<EditText
       android:id="@+id/edit_text"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"         //在页面按比例布局
       android:hint="type something"
       />
 
    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="send"
        />
显示:
___【】
 
若改成
<EditText
       android:id="@+id/edit_text"
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:hint="type something"
       />    //edit_text会占满剩余屏幕
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"
        />
显示:
__________【】
 
 
2.RelativeLayout  //相对布局
在页面布局:
android:layout_alignParentLeft+ android:layout_alignParen Top:左上
android:layout_alignParenRight+ android:layout_alignParen Bottom:右下 
android:layout_centerInParent:居中
 
相对控件布局:
//在button3右上
android:layout_above="@id/button3"
android:layout_toRightOf="@id/button3"
 
// 在button3左下
android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"
 
//与button3左边缘对齐
android:layout_alignLeft="@id/button3"
//与button3上端对齐
android:layout_alignTop="@id/button3"
 
3.TableLayout  //表格布局
 
<TableLayout 
   。。。
android:stretchColumns="1"   //延伸第二格以填充空白   0表示第一格
>
 
<TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="Account:"
            />
        <EditText
            android:id="@+id/account"
            android:layout_height="wrap_content"
            android:hint="Input your account"
           
            />        
    </TableRow>
 
    <TableRow>
        <TextView
            android:layout_height="wrap_content"
            android:text="Password:"
            />
        <EditText
            android:id="@+id/password"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            />
    </TableRow>
 
 
   
    <TableRow>
        <Button
            android:id="@+id/login"
            android:layout_height="wrap_content"
            android:layout_span="2"      //合并单元格
            android:text="login"
            />
    </TableRow>
 
显示:
Account:  ______________
Password:______________
【               login          】
 
2.各控件所占比例可通过 如weightSum="1"(总比例) 和         android:layout_weight="(内填比例)"

 控制

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值