【Android快速入门3】布局简介及例子

目前自学到布局部分,下面演示了不同布局的基本训练,涵盖的内容还是不错的,而且简单易懂,分享给大家。

1.LinearLayout流式布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="开始"
        />
    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:text="返回"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结束"
        />
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="3"
        >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="new"
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:visibility="invisible"
            android:text="newc"
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="news"
            />
    </LinearLayout>

</LinearLayout>

2.RelativeLayout相对布局

<?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" >
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Fight"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="L_f"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:text="R_f"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="Run"
        />
    <Button 
        android:id="@+id/bom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Boom!"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/bom"
        android:layout_alignBaseline="@+id/bom"
        android:text="左"
        />
     <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/bom"
        android:layout_alignBaseline="@+id/bom"
        android:text="右"
        />
      <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:layout_above="@+id/bom"
          android:layout_centerHorizontal="true"
        android:text="上"
        />
       <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
          android:layout_below="@+id/bom"
          android:layout_centerHorizontal="true"
        android:text="下"
        />
    

</RelativeLayout>

3.FrameLayout帧布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        />
    <Button 
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        />
    <Button 
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        />

</FrameLayout>

4.其他不常用布局,比如绝对布局,表格布局

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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:layout_x="150dp"
        android:layout_y="160dp"
        android:text="kakka"
        />

</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:shrinkColumns="0"
    android:collapseColumns="0">
    <TableRow 
        android:layout_height="wrap_content">
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="L1,C0"
                />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="L1,C1"
                />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="L1,C3"
                />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="L1,C4"
                />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="L1,C5"
                />
    </TableRow>
    <TableRow 
        android:layout_width="wrap_content"
        >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="L2,C0"
            />
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_span="2"
            android:text="L2,C1"
            />
    </TableRow>
    

</TableLayout>

 

【Android快速入门3】布局简介及例子,布布扣,bubuko.com

【Android快速入门3】布局简介及例子

上一篇:OSI七层与TCP/IP五层网络架构详解


下一篇:关闭ios虚拟键盘的几种方法