【Android开发学习笔记】【第七课】五大布局-上

概念


Android程序各式各样,依靠的就是布局,先来看看布局都是怎么来的:

 

白色部分就是我们经常用的几种布局,主要说说介绍下面五大布局

FrameLayout

AbsoluteLayout

LinearLayout

RelativeLayout

TableLayout

 

先介绍两种:

 

线性布局-LinearLayout


在一个方向上对齐所有元素。

可以横着、竖着,也可以嵌套,直接看代码吧

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

    <!-- vertical 代表垂直布局 -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="#88AAFF"
        android:gravity="center_horizontal"
        android:text="ABCDEFG"
        android:textSize="14dip" />

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

        <!-- horizontal 代表横向布局 -->

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:layout_weight="0"
            android:background="#22FFFF"
            android:text="hello" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:layout_weight="1"
            android:background="#FF22FF"
            android:text="world" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:layout_weight="2"
            android:background="#2222FF"
            android:text="ni" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="50dip"
            android:layout_weight="3"
            android:background="#FFFF22"
            android:text="hao" />
    </LinearLayout>

</LinearLayout>

 

根据上面的布局和程序运行的结果,可以得到如下结论:

1、android:orientation="vertical" 代表垂直布局   horizontal 则代表横向布局

2、android:gravity="center_horizontal" 这一句话使得 ABCDEFG 居中对齐

3、android:layout_weight="0"  这个值决定了占屏幕的百分比的权重

程序运行结果:

 

看起来对于一般的需求,线性布局就够了。

 

 

相对布局-Relative Layout


听说是布局里面功能最强大的,它的存在是为了适应五花八门的屏幕分辨率。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <AnalogClock
        android:id="@+id/aclock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    <!-- layout_centerInParent="true"    居中显示,将这个控件显示在父窗口的中间位置. -->

    <DigitalClock
        android:id="@+id/dclock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/aclock"
        android:layout_below="@id/aclock"
        android:layout_marginLeft="40px" />

    <!--
        layout_alignLeft="@id/aclock"       将控件的左边缘和给定ID控件的左边缘对齐
        android:layout_below="@id/aclock"   将控件置于给定ID控件之下 
        layout_marginLeft                   定义的控件左边距为40个dip
    -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/aclock"
        android:layout_toLeftOf="@id/dclock"
        android:text="当前时间:" />

    <!--
        android:layout_above="@id/xxx"    将控件置于给定ID控件之上 
        android:layout_toLeftOf           将控件的右边缘和给定ID控件的左边缘对齐
    -->


</RelativeLayout>

 

根据上面注释里面给出来的就是,就可以知道程序运行起来之后是这样样子的:

相对布局果然强大,可以随意布置,下面看一些常用的相对布局使用的属性的含义:

android:layout_above="@id/xxx"            --将控件置于给定ID控件之上
android:layout_below="@id/xxx"            --将控件置于给定ID控件之下
android:layout_toLeftOf="@id/xxx"         --将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx"        --将控件的左边缘和给定ID控件的右边缘对齐
android:layout_alignLeft="@id/xxx"        --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"         --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"       --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"      --将控件的底边缘和给定ID控件的底边缘对齐
android:layout_alignParentLeft="true"     --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"      --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"    --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true"   --将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true"      --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"    --将控件置于水平方向的中心位置
android:layout_centerVertical="true"      --将控件置于垂直方向的中心位置
Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值