Android AbsoluteLayout(绝对)、RelativeLayout(相对)、RTL(RightToLeth)(布局小结二)

来源:互联网 发布:淘宝售假仅退款不退货 编辑:程序博客网 时间:2024/06/06 15:40

AbsoluteLayout(绝对布局)

故名思意,强制固定位置的一种布局方式,但由于Android系统的设备,分辨率过多,采用此布局方式,会造成在各个不同分辨率下的效果都不同,故此,此布局不推荐使用。

代码实现:

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮"        android:layout_x="150dp"     <!--设定x轴位置-->        android:layout_y="150dp"      <!--设定y轴位置-->        /></AbsoluteLayout>

实现效果:

这里写图片描述

当更换分辨率,还是原来的参数,部件所处的位置就发成了变化。

RTL(RightToLeth从右往左,4.0以后的版本才支持) 、布局包含

这个设定从右往左开始,一般用于阿拉伯语这些环境中。
布局包含,意思是,在基布局文件内,包含别的布局文件入内。

效果图:
让组件从右往左开始布局,并且始终保持二个组件之间的距离,再包含一个布局文件(包含未截图。。)
这里写图片描述

实现代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layoutDirection="rtl"  <!--设置布局的是从右往左-->    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮One"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="按钮Two"        android:layout_marginStart="30dp"  <!--不管是从那边开始布局,组件都是开始就30dp-->        /> <!--包含,和jsp内的包含相同,指在本布局中包含其他布局文件,   值为:@布局文件夹名/被包含的布局文件名--> <include layout="@layout/absolutelayout"></include> </LinearLayout>

RelativeLayout(相对布局)

简介:以控件之间相对位置或者相对父容器位置进行排列,相对布局的参数有二种:
1、相对父控件,值:true或者false
2、相对于兄弟控件,通过id’取兄弟控件的属性

相对于父:

相对于父容器 作用 android:layout_alignParentLeft=”true” 与父左边相同 android:layout_alignParentRight=”true” 与父右边相同 android:layout_alignParentTop=”true” 与父上对齐 android:layout_alignParentBotton=”true” 与父下对齐 android:layout_centerInParent=”true” 相对于父,水平、垂直都居中 android:layout_centerHorizontal=”true” 相对于父,水平居中 android:layout_centerVertical=”true” 相对于父,垂直居中

属性代码展示:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮One" android:layout_alignParentRight="true"<!--其他几个方法的代码实现,基本相同,例如:上 android:layout_alignParentTop="true"/></RelativeLayout>

效果图:
这里写图片描述

相对与兄弟组件:

:相对兄弟组件 :注释: android:layout_above=”@id/id名” 指定本控件在id控件之上 android:layout_below=”@id/id名” 位于此ID之下 android:layout_toLeftOf=”@id/id名” 左边与ID控件的左边对齐 android:layout_toRightOf=”@id/id名” 右边与ID控件的右边对齐 android:layout_alignBaseline=”@id/id名” 该控件的baseline和给定ID的baseline对齐 android:layout_alignRight=”@id/id名” 将该控件的右边缘与给定ID控件的右边缘对齐 android:layout_alignLeft=”@id/id名” 将该控件的左边缘与给定ID控件的左边缘对齐 android:layout_alignTop=”@id/id名” 将该控件的顶部边缘与给定ID控件的顶部边缘对齐 android:layout_alignBottom=”@id/id名” 将该控件的底部边缘与给定ID控件的底部缘对齐

其他属性

属性 作用 android:layout_marginLeft 本组件的左距离 android:layout_marginRight 本组件的右距离 android:layout_marginTop 本组件的上距离 android:layout_marginBottom 本组件的下距离 android:layout_alignEnd 将控件对齐给定ID控件的尾部 android:layout_alignParentStart 将控件对齐到父控件的头部 android:layout_alignParentEnd 将控件对齐到父控件的尾部 android:layout_margin=”10dp” 本组件的上下左右都相隔10dp

版本4.2以上相对布局新属性

android:layout_alignStart———————将控件对齐给定ID控件的头部

布局,思维导图

这里写图片描述

阅读全文
0 0
原创粉丝点击