Android 应用开发 常用布局

目录

1.线性布局 LinearLayout

2.相对布局  RelativeLayout

3.网格布局 GridLayout

4.滚动视图 ScrollView


1.线性布局 LinearLayout

线性布局内部视图之间的排列顺序是固定的,要么从左到右排列,要么从上到下排列。在xml文件中,LinearLayout通过属性android:orientation区分两种方向,有从左到右horizontal和从上到下vertical两种。默认为horizontal(水平方向)。

示例

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000" />
    </LinearLayout>

</LinearLayout>

效果:

除了方向之外,线性布局还有权重的概念,指的是线性布局的下级视图各自拥有多大比例的宽和高,通过layout_weight进行设置,一旦设置了这个值,就要求layout_width或layout_height填0dp,哪个值填0dp,下级视图就从对应方向按照权重分割布局。

示例


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#ffffff" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#00ffff"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000" />
    </LinearLayout>

效果

2.相对布局  RelativeLayout

线性布局的下级视图是顺序排列的,另一种相对布局的下级视图位置由其他视图决定。相对布局需要指定参照物,一种是下级视图平级的视图,另一种是该视图的上级视图,也就是它归属的RelativeLayout。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp" >
    
    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#ffffff"
        android:text="我在中间"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#eeeeee"
        android:text="我在水平中间"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#eeeeee"
        android:text="我在垂直中间"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#eeeeee"
        android:text="我跟上级左边对齐"
        android:textSize="11sp"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#eeeeee"
        android:text="我跟上级右边对齐"
        android:textSize="11sp"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#eeeeee"
        android:text="我跟上级顶部对齐"
        android:textSize="11sp"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#eeeeee"
        android:text="我跟上级底部对齐"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_center"
        android:layout_alignTop="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间左边"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_center"
        android:layout_alignBottom="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间右边"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_above_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间上面"
        android:textSize="11sp"
        android:textColor="#000000" />
    
    <TextView
        android:id="@+id/tv_below_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_center"
        android:layout_alignRight="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间下面"
        android:textSize="11sp"
        android:textColor="#000000" />
</RelativeLayout>

效果

3.网格布局 GridLayout

线性布局只支持单行单列的布局方式,网格布局支持多行多列的布局方式。网格布局默认从左到右,从上到下排列,它先从第一行从左往右放置下级视图,塞满之后另起一行放置其余的下级视图。如此循环直至所有视图放置完毕,为了判断能够容纳几行几列,网格布局新增了android:columnCount与android:rowCount两个属性,其中columnCount指定了网格的列数,rowCount指定了网格的行数。网格布局xml示例:

<!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2">

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#ffcccc"
        android:text="浅红色"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#ffaa33"
        android:text="橙色"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#00ff00"
        android:text="绿色"
        android:textColor="#ffffff"
        android:textSize="17sp" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#660066"
        android:text="深紫色"
        android:textColor="#ffffff"
        android:textSize="17sp" />

</GridLayout>

效果:

4.滚动视图 ScrollView

滚动视图也分为垂直方向和水平方向两类,其中垂直滚动视图名为ScrollView,水平滚动视图名为HorizontalScrollView。

(1)在垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。

(2)在水平方向滚动时,layout_width设置为wrap_content,layout_height属性值设置为match_parent。

(3)滚动视图节点下面必须且只能挂着一个子布局节点,否则会在运行时报错。

示例:

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

    <!-- HorizontalScrollView是水平方向的滚动视图,当前高度为200dp -->
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp">

        <!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色 -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <View
                android:layout_width="1000dp"
                android:layout_height="match_parent"
                android:background="#aaffff" />

            <View
                android:layout_width="1000dp"
                android:layout_height="match_parent"
                android:background="#ffff00" />
        </LinearLayout>
    </HorizontalScrollView>

    <!-- ScrollView是垂直方向的滚动视图,当前高度为自适应 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- 垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#00ff00" />

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#ffffaa" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

效果:

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android应用开发详解》 作者:郭宏志 编著 内容简介    本书分为三个部分,包括基础篇、技术篇和应用篇。由浅入深地讲述了Android应用开发的方方面面。    第一篇 基础篇 第1章 Android概述 Android概述,讲述了Android的前生后世、架构和特点、Android Market、应用程序组件和Android与Java ME的区别及联系    第2章 Android开发基础 Android开发基础,讲述了Android开发环境的搭建、Android常用工具的使用和第一个Android应用程序的开发    第二篇 技术篇 第3章 Android中的资源访问 Android 中的资源访问,讲述了如何定义和访问Android中的外部资源。   第4章 Android用户界面 Android 用户界面,讲述了Android中的事件处理机制、布局管理和常用组件的使用。    第5章 Android基本程序单元Activity Android 基本程序单元Activity,讲述了Android中重要组件Activity的创建、启动和生命周期等内容    第6章 Android组件之间的信使Intent Android 组件之间的信使Intent,讲述了Intent对象及其属性、Intent的实现策略和Intent的常见应用   第7章 Android Service组件 Android Service组件,讲述了Android中的后台服务Service的概念、创建和使用,并详细讲解了远程服务的调用    第8章 Android广播事件处理 Broadcast Receiver Android 广播事件处理 Broadcast Receiver,讲述了广播事件处理机制、Notification、NotificationManager和AlarmManager的使用    第9章 Android中的数据存取 Android中的数据存取,讲述了Android的四种数据存取方法:Preference、File、SQLite和Network    第10章 Content Provider Content Provider,讲述了Android不同应用程序之间相互共享数据的机制,包括ContentProvider和ContentResolver    第11章 Android中的多媒体应用 Android中的多媒体应用,讲述了Android的图片应用、音频及视频播放、音频及视频录制和照相机的使用    第12章 Android中的图形图像 Android中的图形图像,讲述了Android中的图片、动画、图形绘制和图形特效    第13章 Android中的互联网应用 Android中的互联网应用,讲述了Android中的各种网络应用,包括Socket、URL、HTTP、Web Service和WebView组件    第14章 Android中的GPS应用 Android中的GPS应用,讲述了LocationManager、LocationProvider、跟踪、定位、Geocoder正逆向编解码和可视化位置服务    第三篇 应用篇 第15章 Android应用案例——移动警务通 Android 应用案例——移动警务通,通过一个真实的商业案例讲解了Android的各种应用,本项目包括信息采集、信息查询、照片上传、GPS定位等综合警务应用    第16章 Android应用案例——雷电游戏 Android 应用案例——雷电游戏,通过一个完整的雷电游戏,讲述了Android的游戏开发思路、框架和具体实现,并比较了Android游戏开发和Java ME游戏开发的异同,成功移植了Java ME游戏API到Android当中    第17章 Android应用案例——备忘录 Android 应用案例——备忘录,通过一个Android基础应用项目综合应用了Android中的各种组件,包括Activity、Service、Broadcast Receiver、ContentProvider、Intent和View的应用    第18章 Android应用案例——无线点餐系统 Android 应用案例——无线点餐系统,通过一个真实的商业案例讲解了Android的各种应用,本项目包括操作员登录、点餐、结算、转台、并台、查台、更新数据和退出系统等餐厅点餐的常用操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值