Linear Layout,Relative Layout

来源:互联网 发布:一个产品50元,淘宝拍摄 编辑:程序博客网 时间:2024/04/25 08:19

Linear Layout

  • android:orientation
  • android:layout_weight

子控件同等大小:android:layout_height 或者 android:layout_width 设为 “0pd”,设置android:layout_weight 为“1”。

例子:

<?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:paddingLeft="16dp"    android:paddingRight="16dp"    android:orientation="vertical" >    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/to" />    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/subject" />    <EditText        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:gravity="top"        android:hint="@string/message" />    <Button        android:layout_width="100dp"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:text="@string/send" /></LinearLayout>

这里写图片描述

Relative Layout

RelativeLayout支持的XML属性:

  • android:gravity 设置布局容器内各个子组件的对齐方式
  • android:ignoreGravity 设置哪个组件不受gravity属性的影响

RelativeLayout提供了一个内部类:RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局容器中子组件的布局分布。
RelativeLayout.LayoutParams中只能设为true、false的XML属性:

  • android:layout_centerHorizontal 是否位于布局容器的水平居中
  • android:layout_centerVertical 是否位于布局容器的垂直居中
  • android:layout_centerInParent 是否位于布局容器的中央位置
  • android:layout_alignParentBottom 是否与布局容器底端对齐
  • android:layout_alignParentLeft 是否与布局容器左边对齐
  • android:layout_alignParentRight 是否与布局容器右边对齐
  • android:layout_alignParentTop 是否与布局容器顶端对齐

RelativeLayout.LayoutParams里属性为其他UI组件ID的XML属性:

  • android:layout_toRightOf 子组件位于给出ID组件的右侧
  • android:layout_toLeftOf 子组件位于给出ID组件的左侧
  • android:layout_above 子组件位于给出ID组件的上方
  • android:layout_below 子组件位于给出ID组件的下方
  • android:layout_alignTop 子组件位于给出ID组件的上边界对齐
  • android:layout_alignBottom 子组件位于给出ID组件的下边界对齐
  • android:layout_alignLeft 子组件位于给出ID组件的左边界对齐
  • android:layout_alignRight 子组件位于给出ID组件的右边界对齐

例子:

<?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"    android:paddingLeft="16dp"    android:paddingRight="16dp" >    <EditText        android:id="@+id/name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="@string/reminder" />    <Spinner        android:id="@+id/dates"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_below="@id/name"        android:layout_alignParentLeft="true"        android:layout_toLeftOf="@+id/times" />    <Spinner        android:id="@id/times"        android:layout_width="96dp"        android:layout_height="wrap_content"        android:layout_below="@id/name"        android:layout_alignParentRight="true" />    <Button        android:layout_width="96dp"        android:layout_height="wrap_content"        android:layout_below="@id/times"        android:layout_alignParentRight="true"        android:text="@string/done" /></RelativeLayout>

这里写图片描述

0 0