我爱男闺蜜30:android 常用的布局

来源:百度文库 编辑:中财网 时间:2024/04/28 04:59:37

android 常用的布局

1、线性布局 LinearLayout:

线性布局是所有布局中最常用的类之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。

举个例子:


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
   
   android:id="@+id/firstText"
  android:text="第一行一行一行一行一行一行一行一行一行一行"
  android:gravity="center_vertical"
  android:textSize="35pt"
  android:background="#aa0000"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingLeft="10dip"
  android:paddingTop="20dip"
  android:paddingRight="30dip"
  android:paddingBottom="40dip"
  android:layout_weight="1"
        android:singleLine="true"/>
   android:id="@+id/secondText"
  android:text="第二行"
  android:gravity="center_vertical"
  android:textSize="15pt"
  android:background="#0000aa"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"/>

2、相对布局 RelativeLayout

相对布局 RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。它灵活性大很多,当然属性也多,操作难度也大,属性之间产生冲突的的可能性也大,使用相对布局时要多做些测试。

举个例子:


 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
   android:id="@+id/label"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Type here:" />
   android:id="@+id/entry"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@android:drawable/editbox_background"
  android:layout_below="@id/label" />