基本布局

一、线性布局

      LinearLayout 又称线性布局,这个布局会将它所包含的控件在线性方向上一次排列。水平横向排列和垂直纵向排列。通过android:orientation属性来指定。vertical表示垂直方向,horizontal表示水平横向。修改activity_main.xml中的代码。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:baselineAligend="false"
    android:divider="2"
    android:measureWithLargestChild="true"
    android:layout_width="100dp"
    android:layout_height="wrap_content">

     再添加几个按钮Button就可以看出LinearLayout布局的效果了。android:baselinetation该属性用来管理该布局管理器与其他的子元素的基线对其方式。android:divider该属性用来管理垂直布局时两个按钮的分隔条。android:measureWithLargestChild 该属性用来管理所有带权重的子元素具有最大子元素的尺寸。

二、相对布局

     RelativeLayout又称作相对布局。和LinearLayout的排列规则不同的是,RelativeLayout显得更加随意一些。它可以通过相对定位的方式让控件出现在布局的任何位置。修改activity_main.xml中的代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button 1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button 2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button 3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="Button 4" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Button 5" />


</RelativeLayout>

android:padding属性指定了控件的四周的内部留出的边距,当然还有上下左右。android:layout_alignParentLeft、android:layout_alignParentRight、android:layout_alignParentBottom、android:layout_alignParentTop、android:layout_centerInParent用于指定位置,上中下左右。还可以根据控件相对于控件定位。假如说相对于Button 3进行定位,则把Button 1、Button 2、Button 4、Button 5、的定位控件依次改为。

android:layout_above="@id/button3"
android:layout_toLeftOf="@id/button3"

android:layout_above="@id/button3"
android:layout_toRightOf="@id/button3"

android:layout_below="@id/button3"
android:layout_toLeftOf="@id/button3"

android:layout_below="@id/button3"
android:layout_toRightOf="@id/button3"

三、帧布局

    FrameLayout又称作帧布局,这种布局没有方便的定位方式,所有的控件都是默认地摆放在布局的左上角。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="20dp" 
    android:foregroundGravity="right"

android:foreground:属性用于设置帧布局的前景图像。android:foregroundGravity属性用于设置前景图像的位置。除了默认的效果还可以使用android:gravity属性来指定控件在布局中的对齐方式。

四、百分比布局

百分比布局可以不再使用wrap_content和match_parent等方式来指定控件的大小,而是直接指定控件在布局中的百分比,这样就可以轻松实现平分布局和任意比例分割布局了。百分比布局属于新增的布局,Android团队将它定义在了support库里面,我们要使用它就得在项目build.gradle中添加百分比布局库的依赖,就能保证在所有Android系统版本上的兼容性了。打开app/buile.gradle文件,在dependencies闭包中添加如下内容。

dependencies {
    compile fileTree(dir:'libs',include:['*.jar'])
    compile 'com.android.support:appcompat-v7:26.1.0'
    compile 'com.android.support:percent:26.1.0'
    testCompile 'junit:4.12'
}

注意:每当修改了gradle里面的文件时,Android Studio 都会弹出一个提示。


这个提示告诉我们,gradle文件自上次同步之后又发生了变化,需要再次同步才能使项目正常工作。此时需要点击Sync Now,然后gradle会开始同步,把我们新添加的百分比布局库引入到项目中。接下来修改activity_main.xml中的代码

<android.support.percent.PercentFrameLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:text="Button 1"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

    <Button
        android:id="@+id/button2"
        android:text="Button 2"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

    <Button
        android:id="@+id/button3"
        android:text="Button 3"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>

    <Button
        android:id="@+id/button4"
        android:text="Button 4"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"/>


</android.support.percent.PercentFrameLayout>

五、自定义布局

     MyLayout又称自定义布局。自定义布局主要重写两个方法:onMeasure()这个是自定义容器的大小,onLayout()这个是写子元素的布局。布局一共有三种计算模式。MeasureSpec.EXACTLY:精确尺寸,MeasuroSpec.AT_MOST:最大尺寸。MeasureSpec.UNSPECIFIED:未指定尺寸。

六、表格布局

    TableLayout 又称表格布局。类似于以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。当是TableRow对象时,可在TableRow下添加子控件,默认情况下,每一个子控件都各占据一列。当为View时:该View将独占一行。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="4"
    android:shrinkColumns="2"
    android:stretchColumns="2"
</TableLayout>
    android:collapseColumns设置第几列为隐藏。android:shrinkColumns属性设置第几列为收缩列。android:stretchColumns属性设置第几列为伸展列。







  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值