Android 五大布局方式详解

 更新时间:2016年09月05日 10:56:52   作者:lingdududu  
本文主要介绍Android 五大布局的知识资料,这里整理了详细的布局资料及实现示例代码,和实现效果图,有兴趣的小伙伴可以参考下

Android中常用的5大布局方式有以下几种:

线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
帧布局(FrameLayout):组件从屏幕左上方布局组件。
表格布局(TableLayout):按照行列方式布局组件。
相对布局(RelativeLayout):相对其它组件的布局方式。
 绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。

 1. 线性布局

线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。

常用的属性:

android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小

第一个实例

①效果图:

 ②核心代码如下:

main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:orientation="vertical" 
 > 
 <EditText 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  /> 
 </LinearLayout> 
 <LinearLayout 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:orientation="horizontal" 
 android:gravity="right" 
 > 
 <!-- android:gravity="right"表示Button组件向右对齐 --> 
 <Button 
  android:layout_height="wrap_content" 
  android:layout_width="wrap_content" 
  android:text="确定" 
  /> 
 <Button 
  android:layout_height="wrap_content" 
  android:layout_width="wrap_content" 
  android:text="取消" 
  /> 
 </LinearLayout> 
</LinearLayout> 

第二个实例

①效果图:

 ②核心代码:

mian.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
 
 <LinearLayout 
 android:orientation="horizontal" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:layout_weight="1"> 
 
 <TextView 
 android:text="red" 
 android:gravity="center_horizontal" 
 android:background="#aa0000" 
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent" 
 android:layout_weight="1" 
 /> 
 <!--android:gravity="center_horizontal"水平居中 --> 
 <!--layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。 
  线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。 
 例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1, 
 那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸; 
 对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度, 
 再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度--> 
 <TextView 
 android:text="Teal" 
 android:gravity="center_horizontal" 
 android:background="#008080" 
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent" 
 android:layout_weight="1"/> 
 
 <TextView 
 android:text="blue" 
 android:gravity="center_horizontal" 
 android:background="#0000aa" 
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent" 
 android:layout_weight="1" 
 /> 
 
 <TextView 
 android:text="orange" 
 android:gravity="center_horizontal" 
 android:background="#FFA500" 
 android:layout_width="wrap_content" 
 android:layout_height="fill_parent" 
 android:layout_weight="1" 
 /> 
  
 </LinearLayout> 
 <LinearLayout 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:layout_weight="1"> 
 
 <TextView 
 android:text="row one" 
 android:textSize="15pt" 
 android:background="#aa0000" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="1" 
 /> 
 <!-- --> 
 <TextView 
 android:text="row two" 
 android:textSize="15pt" 
 android:background="#DDA0DD" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="1" 
 /> 
 
 <TextView 
 android:text="row three" 
 android:textSize="15pt" 
 android:background="#008080" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="1" 
 /> 
 <TextView 
 android:text="row four" 
 android:textSize="15pt" 
 android:background="#FFA500" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:layout_weight="1" 
 /> 
 </LinearLayout> 
</LinearLayout> 

2. 帧布局

帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。

 简单的例子

①效果图:

 

② 核心代码:

main.xml
<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <TextView 
 android:layout_width="300dp" 
 android:layout_height="300dp" 
 android:background="#00BFFF"  
 /> 
 <TextView 
 android:layout_width="260dp" 
 android:layout_height="260dp" 
 android:background="#FFC0CB"  
 /> 
 <TextView 
 android:layout_width="220dp" 
 android:layout_height="220dp" 
 android:background="#0000FF"  
 /> 
</FrameLayout> 

 3.表格布局

表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。

表格布局常用的属性如下:

android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数

 简单的列子:

①效果图:

② 核心代码: 

main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 > 
 <TableRow> 
 <Button 
  android:text="Button1" 
  /> 
 <Button 
  android:text="Button2" 
  /> 
 <Button 
  android:text="Button3" 
  /> 
 </TableRow> 
 <TableRow> 
 <Button 
  android:text="Button4" 
  /> 
 <Button 
  android:layout_span="2" 
  android:text="Button5" 
  /> 
 </TableRow> 
 
</TableLayout> 

 4.相对布局

相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。

相对布局常用属性请参考我博客的:https://www.jb51.net/article/47434.htm

简单的例子

①效果图:

② 核心代码:

main.xml
 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:padding="10px" 
 > 
 <TextView 
 android:id="@+id/tev1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginBottom="30dp" 
 android:text="Please Type Here:" 
 /> 
 <EditText 
 android:id="@+id/tx1" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_below="@id/tev1" 
 /> 
 <Button 
 android:id="@+id/btn1" 
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:layout_below="@id/tx1" 
 android:layout_alignParentRight="true" 
 android:text="确定" 
 /> 
 <Button 
 android:id="@+id/btn2" 
 android:layout_height="wrap_content" 
 android:layout_width="wrap_content" 
 android:layout_below="@id/tx1" 
 android:layout_toLeftOf="@id/btn1" 
 android:layout_marginRight="30dp" 
 android:text="取消" 
 /> 
</RelativeLayout> 

5. 绝对布局

绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。

以上就是对 Android 五大布局的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

相关文章

  • RxJava两步打造华丽的Android引导页

    RxJava两步打造华丽的Android引导页

    如今,移动应用对首次使用的用户呈现欢迎页已经越来越普遍了。这样做的目的就是向用户介绍并展示我们的应用。本文给Android开发的引导页面提供了很多参考,非常值得一读。
    2016-07-07
  • Android解析json数组对象的方法及Apply和数组的三个技巧

    Android解析json数组对象的方法及Apply和数组的三个技巧

    这篇文章主要介绍了Android解析json数组对象的方法及Apply和数组的三个技巧的相关资料,需要的朋友可以参考下
    2015-12-12
  • Android子线程与更新UI问题的深入讲解

    Android子线程与更新UI问题的深入讲解

    首先和其他许多的GUI库一样,Android的UI线程是不安全的。所以下面这篇文章主要给大家介绍了关于Android子线程与更新UI问题的相关资料,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2019-03-03
  • Android JobScheduler详细介绍

    Android JobScheduler详细介绍

    JobScheduler是Android 5.0引入的系统服务,它可以根据网络状态、充电状态、电量和存储状况等来触发相应的JobService执行任务,它支持多条件组合、持久性任务,以及在API 21以上版本的Android系统中使用,对Android JobScheduler相关知识感兴趣的朋友跟随小编一起看看吧
    2024-09-09
  • 详解android 中文字体向上偏移解决方案

    详解android 中文字体向上偏移解决方案

    这篇文章主要介绍了详解android 中文字体向上偏移解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • Android Doze模式启用和恢复详解

    Android Doze模式启用和恢复详解

    这篇文章主要介绍了Android Doze模式启用和恢复功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • Android 实现圆角图片的简单实例

    Android 实现圆角图片的简单实例

    这篇文章主要介绍了Android 实现圆角图片的简单实例的相关资料,Android 圆角图片的实现形式,包括用第三方、也有系统,需要的朋友可以参考下
    2017-07-07
  • Flutter开发技巧RadialGradient中radius计算详解

    Flutter开发技巧RadialGradient中radius计算详解

    这篇文章主要为大家介绍了Flutter小技巧RadialGradient 中 radius 的计算详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android实现沉浸式导航栏实例代码

    Android实现沉浸式导航栏实例代码

    通过本文给大家分享android实现沉浸式导航栏实例代码,代码非常实用,需要的朋友可以参考下
    2016-05-05
  • Android TV开发:使用RecycleView实现横向的Listview并响应点击事件的代码

    Android TV开发:使用RecycleView实现横向的Listview并响应点击事件的代码

    这篇文章主要介绍了Android TV开发:使用RecycleView实现横向的Listview并响应点击事件的代码,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧
    2018-05-05

最新评论