您的位置:首页 > 移动开发 > Android开发

Android开发 布局

2015-10-25 13:58 477 查看

1.LinearLayout线性布局

LinearLayout是最常用的布局,在这个布局中我们需要注意的地方orientation属性,horizontal(水平排列),vertical(垂直排列),在LinearLayout布局中的控件,尽可能的使用layout_weight属性,来做排列或者手机适配性开发。
horizontal排列,里边控件宽度1:1:1
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
vertical排列:里边控件高度1:1:1
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />

<Button
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />

<Button
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>


2,RelativeLayout相对布局

RelativeLayout相对布局是使用最广泛的一种布局,现在的手机界面很多都比较复杂,所以相对布局用到的很多。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />

<Button
android:id="@+id/btn_middle"
android:layout_width="wrap_content"
android:layout_he
4000
ight="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/btn_middle"
android:layout_below="@id/btn_middle"
android:text="Button" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />

</RelativeLayout>


其他常用属性:直接转载了别的博主的了http://www.cnblogs.com/sevenyuan/archive/2010/09/16/1827940.html

// 相对于给定ID控件

android:layout_above 将该控件的底部置于给定ID的控件之上;

android:layout_below 将该控件的底部置于给定ID的控件之下;

android:layout_toLeftOf    将该控件的右边缘与给定ID的控件左边缘对齐;

android:layout_toRightOf  将该控件的左边缘与给定ID的控件右边缘对齐;

 

android:layout_alignBaseline  将该控件的baseline与给定ID的baseline对齐;

android:layout_alignTop        将该控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;

android:layout_alignLeft        将该控件的左边缘与给定ID的左边缘对齐;

android:layout_alignRight      将该控件的右边缘与给定ID的右边缘对齐;

// 相对于父组件

android:layout_alignParentTop      如果为true,将该控件的顶部与其父控件的顶部对齐;

android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;

android:layout_alignParentLeft      如果为true,将该控件的左部与其父控件的左部对齐;

android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;

// 居中

android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;

android:layout_centerVertical     如果为true,将该控件的置于垂直居中;

android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;

// 指定移动像素

android:layout_marginTop      上偏移的值;

android:layout_marginBottom 下偏移的值;

android:layout_marginLeft   左偏移的值;

android:layout_marginRight   右偏移的值;

3,FrameLayout布局

帧布局中的每一个组件都代表一个画面,默认以屏幕左上角作为( 0,0 )坐标,按组件定义的先后顺序依次逐屏显示 , 后面出现的会覆盖前面的画面 。 用该布局可以实现动画效果 。帧布局的大小由最大的子控件决定,帧布局在动画中应用比较多。

常用属性:foreground 设置该帧布局容器的前景图像 foregroundGravity设置前景图像的显示位置

学习帧布局:转载,http://blog.csdn.net/lihongjian944043440/article/details/7055589  和 http://www.it165.net/pro/html/201403/10735.html

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF6143" />

<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#7BFE00" />

<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFF00" />

</FrameLayout>

4,TableLayout布局

格布局类似Html里面的Table。每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素,设定他的对齐方式 android:gravity="",如:

<TableLayout > 是顶级元素,说明采用的是表格布局

<TableRow> 定义一个行

<TextView > 定义一个单元格的内容

参考http://blog.csdn.net/beyond0525/article/details/8841139 
常用属性:



在第二个例子里的EditText代码改成这样:

<TableRow>

        <EditText android:layout_span="2"/>

        <EditText android:layout_column="2"/>

</TableRow>

Layout_span=”2” 伸展2倍

Layout_column=”2”  位置固定到第三列

5,AbsoluteLayout布局

绝对位置的布局;也可以叫做坐标布局,也就是指定元素的绝对位置(或者叫绝对坐标值)。这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差。

<AbsoluteLayout 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=".MainActivity" >

<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="绝对布局的使用"
android:layout_x="80px"
android:layout_y="80px"/>

<TextView
android:id="@+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="2.3之后废除了"
android:layout_x="120px"
android:layout_y="120px"/>

<ImageView
android:id="@+id/img1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="80px"
android:layout_y="180px"
android:src="@drawable/ic_launcher"/>

</AbsoluteLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  布局