Android 新手入门(2)-界面布局

本文介绍了四种不同的Android布局类型:线性布局(LinearLayout),相对布局(RelativeLayout),表格布局(TableLayout)及绝对布局(AbsoluteLayout)。每种布局都通过XML示例详细说明了其特性与使用方式。

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

一,线性布局(LinearLayout)

以线性方向显示它的子视图(view)元素,垂直或水平,

android:orientation值为“vertical”垂直排列,"horizontal"即为水平排列,

 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button1" android:text="线"  ></Button> 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button2" android:text="性"  ></Button> 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button3" android:text="布"  ></Button> 
    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
        android:id="@+id/button4" android:text="局"  ></Button> 
 
</LinearLayout> 

 \


 

二,相对布局(RelativeLayout)

 <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/button1" android:text="相"   
        android:layout_alignParentLeft="true"></Button> 
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:id="@+id/button2" android:text="对"   
        android:layout_below="@+id/button1"></Button> 
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/button3" android:text="布"   
        android:layout_below="@+id/button2" 
        android:layout_toLeftOf="@+id/button4" android:layout_alignTop="@+id/button4"></Button> 
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:id="@+id/button4" android:text="局"   
        android:layout_below="@+id/button2" 
        android:layout_alignParentRight="true"></Button> 
 
</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/button1" android:text="相" 
     android:layout_alignParentLeft="true"></Button>
 <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
     android:id="@+id/button2" android:text="对" 
     android:layout_below="@+id/button1"></Button>
 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/button3" android:text="布" 
     android:layout_below="@+id/button2"
     android:layout_toLeftOf="@+id/button4" android:layout_alignTop="@+id/button4"></Button>
 <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
     android:id="@+id/button4" android:text="局" 
     android:layout_below="@+id/button2"
     android:layout_alignParentRight="true"></Button>

</RelativeLayout>

 

\

 

三,表格布局(TableLayout)


 

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="0,1,2"> 
    <TableRow> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button1" android:text="表格布局------"  
            android:layout_column="0"></Button> 
        <Button android:layout_width="fill_parent" android:layout_height="wrap_content" 
            android:id="@+id/button2" android:text="表格布局------"  
            android:layout_column="1"></Button> 
    </TableRow> 
    <TableRow> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button3" android:text="表格布局------"  
            android:layout_column="1"></Button> 
    </TableRow> 
    <TableRow> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button4" android:text="表格布局------"  
            android:layout_column="2"></Button> 
    </TableRow> 
</TableLayout > 
<?xml version="1.0" encoding="utf-8"?>
<TableLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,1,2">
 <TableRow>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button1" android:text="表格布局------" 
      android:layout_column="0"></Button>
  <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:id="@+id/button2" android:text="表格布局------" 
      android:layout_column="1"></Button>
 </TableRow>
 <TableRow>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button3" android:text="表格布局------" 
      android:layout_column="1"></Button>
 </TableRow>
 <TableRow>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button4" android:text="表格布局------" 
      android:layout_column="2"></Button>
 </TableRow>
</TableLayout >

 \


 

四,绝对布局(AbsoluteLayout)

<?xml version="1.0" encoding="utf-8"?> 
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:shrinkColumns="0,1,2"> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button1" android:text="绝对布局"></Button> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button2" android:text="绝对布局"    
            android:layout_x="160dip"></Button> 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button3" android:text="绝对布局"    
            android:layout_y="50dip"></Button> 
 
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
            android:id="@+id/button4" android:text="绝对布局"    
            android:layout_x="160dip" android:layout_y="150dip"></Button> 
</AbsoluteLayout> 
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="0,1,2">
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button1" android:text="绝对布局"></Button>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button2" android:text="绝对布局" 
      android:layout_x="160dip"></Button>
  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button3" android:text="绝对布局" 
      android:layout_y="50dip"></Button>

  <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
      android:id="@+id/button4" android:text="绝对布局" 
      android:layout_x="160dip" android:layout_y="150dip"></Button>
</AbsoluteLayout>

 

\

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值