Android布局

来源:互联网 发布:meanshift 聚类算法 编辑:程序博客网 时间:2024/06/08 18:47

布局就是把界面中的控件按照某种规律摆放在指定的位置,主要是为了解决应用程序在不同手机中显示的问题。
这里写图片描述
今天来介绍下Android系统中为我们提供的五大布局:LinearLayout(线性布局)、RelativeLayout(相对布局).FrameLayout(帧布局)、GridLayout(网格布局)、TablelLayout(表格布局)。

1.LinearLayout线性布局
这里写图片描述
代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"android:orientation="vertical"    tools:context="com.example.asus.myapplication.MainActivity">    <LinearLayout        android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="左上按钮"            />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="right"            >            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="右上按钮"                />        </LinearLayout>    </LinearLayout>    <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="居中按钮"        />        </LinearLayout>    <LinearLayout        android:layout_weight="1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="bottom"        >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="左上按钮"            />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="right"            >            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="右上按钮"                />        </LinearLayout>    </LinearLayout></LinearLayout>

2.RelativeLayout相对布局
这里写图片描述

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.android2.MainActivity"    android:background="#000000">    <TextView        android:layout_width="100dp"        android:layout_height="80dp"        android:text="红色"        android:textSize="40sp"        android:background="#ff0000"        android:gravity="center"        /><TextView        android:layout_width="130dp"        android:layout_height="80dp"        android:text="橙色"        android:textSize="40sp"        android:background="#F1A200"        android:gravity="center"        android:layout_centerHorizontal="true"        /><TextView        android:layout_width="130dp"        android:layout_height="80dp"        android:text="黄色"        android:textSize="40sp"        android:background="#FFFF00"        android:gravity="center"        android:layout_alignParentRight="true"        /><TextView        android:layout_width="130dp"        android:layout_height="80dp"        android:text="蓝色"        android:textSize="40sp"        android:background="#0000FF"        android:gravity="center"        android:layout_centerInParent="true"        android:id="@+id/tv_main_bule"        android:layout_margin="20dp"        /><TextView        android:layout_width="130dp"        android:layout_height="80dp"        android:text="绿色"        android:textSize="40sp"        android:background="#00FF00"        android:gravity="center"        android:layout_toLeftOf="@id/tv_main_bule"        android:layout_centerVertical="true"        /><TextView        android:layout_width="130dp"        android:layout_height="80dp"        android:text="深蓝色"        android:textSize="40sp"        android:background="#4C0485"        android:gravity="center"        android:layout_toRightOf="@id/tv_main_bule"        android:layout_alignBottom="@id/tv_main_bule"        /><TextView        android:layout_width="match_parent"        android:layout_height="80dp"        android:text="紫色"        android:textSize="40sp"        android:background="#EF82EF"        android:gravity="center"        android:layout_alignParentBottom="true"        /></RelativeLayout>

3.TableLayout表格布局
这里写图片描述

<?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:background="#000000">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是第一个lay的第一行"        android:background="#AF2220"        />        <TableRow>            <TextView                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="2行1列"                android:background="#AD0000"                android:layout_weight="1"                />            <TextView                android:layout_width="180dp"                android:layout_height="wrap_content"                android:background="#FFFFFF"                android:text="2行2列" />            <TextView                android:layout_width="150dp"                android:layout_height="wrap_content"                android:text="2行3列"                android:background="#AD00AD"                />        </TableRow>    <TableLayout        android:collapseColumns="1"        >    <TableRow>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="第二个layout的第1行第1列"            android:background="#45ED2E"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="第二个layout的第1行第2列"            android:background="#9C00FF"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="第二个layout的第1行第3列"            android:background="#9C00FF"            />    </TableRow>    <TableRow>        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="1行第1列"            android:background="#20FF00"            /><TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="1行第2列"            android:background="#aa00ff"            />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="1行第3列"            android:background="#A02EEC"            />    </TableRow>    </TableLayout></TableLayout>

4.GridLayout网格布局4.0,计算器形式
这里写图片描述

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <GridLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:rowCount="6"        android:columnCount="4"        >        <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"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="3"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="/"            />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="4"            />     <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="5"            />     <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="6"            />     <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="*"            />     <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="7"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="8"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="9"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="-"            />    <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="0"        android:layout_columnSpan="2"        android:layout_gravity="fill_horizontal"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="."            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="+"        android:layout_rowSpan="2"        android:layout_gravity="fill_vertical"            />    <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="="        android:layout_columnSpan="3"        android:layout_gravity="fill_horizontal"            />    </GridLayout></LinearLayout>

5.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">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text=""        android:background="#ff0000"        />    <TextView        android:layout_width="510dp"        android:layout_height="290dp"        android:layout_gravity="center"        android:text=""        android:background="#FFA500"        />    <TextView        android:layout_width="450dp"        android:layout_height="260dp"        android:layout_gravity="center"        android:text=""        android:background="#FFFF00"        />    <TextView        android:layout_width="380dp"        android:layout_height="220dp"        android:layout_gravity="center"        android:background="#008000"        />    <TextView        android:layout_width="310dp"        android:layout_height="190dp"        android:layout_gravity="center"        android:background="#E0FFFF"        />    <TextView        android:layout_width="260dp"        android:layout_height="150dp"        android:layout_gravity="center"       android:background="@drawable/a1"        /></FrameLayout>
原创粉丝点击