Android 线性布局和相对布局

来源:互联网 发布:windows xp自带壁纸 编辑:程序博客网 时间:2024/05/16 16:13
  • 线性布局(LinearLayout):该标签下的所有子元素会根据orientation属性的值来决定是按行或者是按列来逐个显示。
  • 相对布局(RelativeLayout),按照各子元素之间的位置关系完成布局。

    线性 布局中

 LinearLayout  是一个线性容器 用来包裹基本的显示控件(如:button等)。 容器也有大小 指定宽度和高度。 线性容器指的是容器中的控件是一个挨着一个排列的,它有两种排列方式:一种是横向排列  一种是纵向排列,(它的默认排列方式是横向排列)  横向排列 android:orientation="horizontal"  纵向排列 android:orientation="vertical"线性布局中内所有元素的对齐方式:将所有的子控件水平居中  android:gravity="center_vertical"将所有的子控件垂直居中  android:gravity="center_horizontal"将所有的子控件排列到中心 android:gravity="center"将所有的子控件左对齐      android:gravity="left"将所有的子控件左对齐     android:gravity="right"将所有的子控件上对齐     android:gravity="top"将所有的子控件下对齐    android:gravity="bottom"<!-- 创建线性布局 -->       <LinearLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="#c1d2c3"        android:orientation="vertical"        android:gravity="center">     <Button           android:layout_width="100dp"          android:layout_height="50dp"          android:background="#00e500"          android:text="button1"/>         <Button           android:layout_marginTop="20dp"          android:layout_width="100dp"          android:layout_height="100dp"          android:background="#00a2c3"          android:text="button2"/>    </LinearLayout>

线性布局 如图所示

相对 布局中

相对布局对齐方式(子控件相对于父控件,在子控件上设置)设置相对布局中 单个控件 垂直居中android:layout_centerHorizontal="true"设置相对布局中 单个控件水平居中android:layout_centerVertical="true"设置相对布局中 单个控件居中android:layout_centerInParent="true"设置相对布局中 单个控件 左边对齐android:layout_alignParentLeft="true"设置相对布局中 单个控件 右边对齐android:layout_alignParentRight="true"设置相对布局中 单个控件 顶部对齐android:layout_alignParentTop="true"设置相对布局中 单个控件 底部对齐android:layout_alignParentBottom="true"android:id="@+id/button_1"     是为控件添加一个身份标识  ID android:layout_above="@id/button_1" 是相对布局中 把当前控件放到id为 button_1的上面android:layout_below="@id/button_1" 是相对布局中 把当前控件放到id为 button_1的下面android:layout_toLeftOf="@id/button_1"是相对布局中 把当前控件放到id为 button_1的左面android:layout_toRightOf="@id/button_1"是相对布局中 把当前控件放到id为 button_1的右面<!-- 创建相对布局 -->    <RelativeLayout         android:layout_width="match_parent"        android:layout_height="match_parent"      android:background="#d1d2c3"      android:gravity="center" >      <Button           android:id="@+id/button1"          android:layout_width="100dp"          android:layout_height="50dp"          android:background="#00e500"          android:text="button1" />      <Button          android:layout_below="@id/button1"          android:layout_marginTop="20dp"          android:layout_width="100dp"          android:layout_height="100dp"          android:background="#00a2c3"          android:text="button2"/>    </RelativeLayout>

相对布局 如图所示

注:上面两个虽视图相同 但其中代码结构不同

match_parent 填充窗体wrap_content 包裹内容体例如:android:layout_width="match_parent"        宽度填充窗体      android:layout_height="wrap_content"       高度包裹内容体 android:background="#c1d2c3"  设置 背景颜色android:text="确定"                 设置文字android:textColor="#000000"         设置文字的颜色android:textSize="12sp"          设置文字的大小android:textStyle="bold"             设置字体加粗android:hint="QQ号/手机号/邮箱"      设置提示文字(输入框中指定的提示语)  android:textColorHint="#d3d3d3"      设置提示文字的颜色android:src="@mipmap/picture"        用来加载图片  设置外部 间距:设置控件距离外部的左边 10dp          android:layout_marginLeft="10dp"设置控件距离外部的右边 10dp          android:layout_marginRight="10dp"设置控件距离外部的上边 10dp          android:layout_marginTop="10dp"设置控件距离外部的下边 10dp          android:layout_marginBottom="10dp"同时设置控件距离外部边距 的上下左右 为10dp    android:layout_marging="10dp"设置内部间距设置控件内部所有的内容距离该控件底部10dp    android:paddingBottom="10dp"设置控件内部所有的内容距离该控件左边10dp    android:paddingLeft="10dp"设置控件内部所有的内容距离该控件右边10dp    android:paddingRight="10dp"设置控件内部所有的内容距离该控件顶部10dp    android:paddingTop="10dp"同时设置 上 下 左 右  的内边距 为10dp      android:padding="10dp"
1 0
原创粉丝点击