android学习系列五 布局

本文详细介绍了Android中常见的布局视图,包括LinearLayout、TableLayout及RelativeLayout的使用方法,并通过实例展示了如何利用这些布局来构建界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LinearLayout 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/first"
        android:text="第一行"
        android:gravity="top"
        android:background="#aa0000"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:paddingRight="20dip"
        android:paddingTop="30dip"
        android:paddingBottom="40dip"
        android:layout_weight="1"
        android:singleLine="true"
        />
    <TextView
         android:id="@+id/second"
        android:text="第二行"
        android:gravity="top"
        android:background="#0000aa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
         />
</LinearLayout>
TableLayout的使用
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  
    android:layout_width="fill_parent"   
  
    android:layout_height="fill_parent">   
    
   <TableRow >
       <TextView 
           android:text="column1"
           android:gravity="left"
           android:padding="3dip"
           />
            <TextView 
           android:text="column2"
           android:gravity="center_vertical"
              android:padding="3dip"
           />
                 <TextView 
           android:text="column3"
           android:gravity="right"
              android:padding="3dip"
           />
   </TableRow>
</TableLayout>

嵌套的使用方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:orientation="vertical"
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent">   
    
   <LinearLayout 
       android:orientation="horizontal"
         android:layout_width="fill_parent"   
       android:layout_height="fill_parent"
       android:layout_weight="1"
       >
       <TextView 
           android:text="red"
           android:background="#aa0000"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           />
        <TextView 
           android:text="green"
           android:background="#00aa00"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           />
         <TextView 
           android:text="blue"
           android:background="#0000aa"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           />
       
   </LinearLayout>
   <LinearLayout 
       android:orientation="vertical"
         android:layout_width="fill_parent"   
       android:layout_height="fill_parent"
       android:layout_weight="1"
       >
       <TextView 
           android:text="red"
           android:background="#aa0000"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           />
        <TextView 
           android:text="green"
           android:background="#00aa00"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           />
         <TextView 
           android:text="blue"
           android:background="#0000aa"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           />
       
   </LinearLayout>
</LinearLayout>

RelativeLayout

android:layout_below将该控件置于给定id的控件之下

android:layout_above将该控件置于给定id的控件之上

android:layout_toLeftOf 将该控件右边缘与给定id控件左边缘对齐

android:layout_toRightOf  将该控件左边缘与给定id控件右边缘对齐


android:layout_alignLeft=""  将该控件的左边缘与给定id控件的左边缘对其
android:layout_alignTop=""
android:layout_alignRight=""
android:layout_alignBottom=""

举例如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">
    
    <TextView android:id="@+id/label"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="type here:"/>
    <EditText
        android:layout_alignBaseline=""
      
        android:inputType="text"
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:text="OK"
        />
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel"
        />
        
    
</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值