布局管理器使得app可以适应于所有分辨率和任何尺寸的手机(绝对布局管理器已过期)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:gravity="center" android:ignoreGravity="@id/user" tools:context=".MainActivity"> <!--xmlns:android="http://schemas.android.com/apk/res/android"表示Android命名空间的,属性值是固定的写法, 写什么布局管理器都是这个属性值 xmlns:tools="http://schemas.android.com/tools" 定义工具命名空间的,不使用可以删除掉,如果删除,就不能在后面出现以tools出现的属性 无论些什么布局管理器,开头两句都是完完全全一样的 layout_width设置组件宽度 layout_height设置组件高度 paddingBotton paddingTop paddingRight paddingLeft 设置内边距的,可以是具体数值,也可以是使用尺寸资源来定义 tools:context=".MainActivity"指向使用这个布局的activity,不会被打包进apk文件的,不用也可以删除掉 gravity设置其下组件摆放方式的,不要和layout_gravity混淆 ignoreGravit用于设置哪个组件不受gravity控制,属性值为id值 为了更好的来控制子组件分布(摆放),相对管理器还提供了一个内部类LayoutParams,通过这个类的xml属性,我们可以更好的控制 子组件的分布方式,这些属性用于布局管理器的子组件标签中,不像前面用于布局管理器标签中。 如layout_above layout_below layout_toLeftOf layout_toRightOf 和 layout_alignParentBottom layout_alignParentTop layout_alignParantLeft layout_alignRight 和layout_alignBotton。。。(和它类似 的三个就略过了) 和layout_centerHorizontal layout_centerInParent layout_centerVertical --> <TextView android:id="@+id/user" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello" android:textSize="20sp"/> <TextView android:id="@+id/user1" android:layout_below="@id/user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="明日" android:textSize="20sp"/> </RelativeLayout>
实例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:background="@mipmap/img01" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".RelativeActivity"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发现widget的新版本,确认跟新吗" android:layout_centerInParent="true"/> <Button android:id="@+id/btn1" android:layout_below="@id/textview" android:layout_alignRight="@id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="现在跟新"/> <Button android:id="@+id/btn2" android:text="以后再说" android:layout_below="@id/textview" android:layout_toLeftOf="@id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>