Android布局

来源:互联网 发布:淘宝宝贝宣言 编辑:程序博客网 时间:2024/06/04 17:53

1.LinearLayout线性布局

布局特点:放主要提供控件水平或者垂直排列的模型,每个子组件都是以垂直或水平的方式来线性排布.(默认是垂直)。

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"    tools:context="com.zking.layout.MainActivity">    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="按钮1"        android:layout_weight="2"        />    <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="按钮2"        android:layout_weight="1"        /></LinearLayout>


2.Relative Layout(相对布局

布局特点:为某一个组件为参照物,来定位下一个组件的位置的布局方式。

<?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"    tools:context="com.zking.g160628_android03_layout2.MainActivity">    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="#ff0000"        android:gravity="center"        android:textColor="#ffffff"        android:text="Red" />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="#FFA600"        android:gravity="center"        android:textColor="#ffffff"        android:layout_centerHorizontal="true"        android:text="Orange"/>    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="#FFFF00"        android:gravity="center"        android:layout_alignParentRight="true"        android:textColor="#ffffff"        android:text="Yellow" />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="#0000ff"        android:gravity="center"        android:textColor="#ffffff"        android:layout_centerInParent="true"        android:id="@+id/tv_main_blue"        android:layout_margin="10dp"        android:text="Blue" />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="#00ff00"        android:gravity="center"        android:textColor="#ffffff"        android:layout_toLeftOf="@id/tv_main_blue"        android:layout_alignBottom="@id/tv_main_blue"        android:text="Green" />    <TextView        android:layout_width="50dp"        android:layout_height="50dp"        android:background="#4A0084"        android:gravity="center"        android:textColor="#ffffff"        android:layout_toRightOf="@id/tv_main_blue"        android:layout_centerVertical="true"        android:text="Indigo" />    <TextView        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="#EF82EF"        android:gravity="center"        android:textColor="#ffffff"        android:layout_alignParentBottom="true"        android:text="Violet" /></RelativeLayout>


3.AbsoluteLayout(绝对布局

布局特点:采用坐标轴的方式定位组件,左上角是(0,0)点,往右x轴递增,

                  往下Y轴递增,组件定位属性为android:layout_x和 android:layout_y来确定坐标。

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绝对布局"        android:layout_x="150dp"        android:layout_y="150dp"        />    <include layout="@layout/public_title"></include></AbsoluteLayout>

4.FrameLayout(帧布局

布局特点:类似于PS中图层的概念,为每个加入其中的组件创建单独的帧,看上去像是组件叠加到一起

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/a3"        android:scaleType="fitXY"        />    <ImageView        android:layout_width="50dp"        android:layout_height="50dp"        android:src="@drawable/a2"        android:layout_gravity="center"        /></FrameLayout>


5.TableLayout(表格布局

布局特点:类似Html里的Table.使用TableRow来布局,其中TableRow代表一行,

               TableRow的每一个视图组件代表一个单元格。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="表1"        />    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:stretchColumns="0"        android:shrinkColumns="1"        >        <TableRow>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="改列可伸展"                />            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="改列可收缩"                />        </TableRow>        <TableRow>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="我向行方向伸展,我可以很长aaaaaaa"                />            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="我向列方向收缩,我可以很深"                />        </TableRow>    </TableLayout>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="表2"        />    <TableLayout        android:layout_width="300dp"        android:layout_height="wrap_content"        android:stretchColumns="1"        android:shrinkColumns="0,2"        >        <TableRow>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="第0列"                />            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="第1列"                />            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="第2列"                />        </TableRow>        <TableRow>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="我被指定在第1列"                android:layout_column="1"                />        </TableRow>        <TableRow>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="我跨1到2列,不信你瞅瞅"                android:layout_column="1"                android:layout_span="2"                />        </TableRow>    </TableLayout>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="表3"        />    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:stretchColumns="0,1,2"        >        <TableRow>            <Button                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="一"                android:layout_weight="1"                />            <Button                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="两字"                android:layout_weight="1"                />            <Button                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:text="三个字"                android:layout_weight="1"                />        </TableRow>    </TableLayout></LinearLayout>
Android 4.0 两中新布局

     1.Grid Layout(网格布局)

<?xml version="1.0" encoding="utf-8"?><GridLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:rowCount="3"    android:columnCount="4"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="1"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="2"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="3"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="4"        android:layout_rowSpan="2"        android:layout_gravity="fill_vertical"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="5"        android:layout_columnSpan="2"        android:layout_gravity="fill_horizontal"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="6"        /></GridLayout>

      2.RTl(从右往左布局)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="match_parent"    android:layout_height="match_parent"    android:layoutDirection="rtl"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="文本1"        android:background="#ff0000"        android:textSize="30sp"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="文本2"        android:background="#00ff00"        android:textSize="30sp"        android:layout_marginStart="50dp"        /></LinearLayout>














原创粉丝点击