Android 五大布局

一、绝对布局(AbsoluteLayout

    1、只根据控件相对Activity的原点0,0坐标进行位置的设置,每个控件的位置对别的控件的位置没有任何影响

    2、在Android2.3.3以后基本被废弃

 

二、帧布局(也叫框架布局,FrameLayout

    1、目的是为了叠加效果

    2、一般进行居中等简单设置,不对位置进行过于精确的定位

 

小知识:外边距(margin)和内边距(padding

 

、表格布局(TableLayout

 

    1TableLayout可以设置整个内容垂直居中,使用属性android:gravity="center_vertical"

    2、单个控件占一行,最好也要包含在TableRow标签中,

不然为该控件设置的宽度是无效的,因为没包含在TableRow标签中它默认占满整行

    3、表格布局时,如果第二行是三列,第一行想只有一列,必须做合并

         合并是在控件中做的,合并属性是android:layout_span

 

**实际开发时要注意:

    

    1、因为实际手机屏幕大小有很多种,我们设计界面时,以应用能运行的最小屏幕作为设计目标,各种大小要事先计算

    2、希望大屏幕里效果也要好,诀窍是:

        2.1 整体布局在水平和垂直上都能居中

        2.2 控件设定物理大小

 

、线性布局(LinearLayout

 

    1、横向或纵向

    2、嵌套实现一些效果

 

 

、相对布局(RelativeLayout

    

    1、灵活,能实现几乎所有嵌套布局能实现的效果,但是需要清晰的设计思路

    2、控件的属性非常多

 

5.1控件的相对位置

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

android:layout_below 将该控件的顶部至于给定ID的控件之下

android:layout_toLeftOf 将该控件位于给定ID的控件的左边

android:layout_toRightOf 将该控件位于给定ID的控件的右边

 

 

 

5.2控件的Margin属性

android:layout_marginBottom 该控件离某元素底边缘的距离

android:layout_marginLeft 该控件离某元素左边缘的距离

android:layout_marginRight 该控件离某元素右边缘的距离

android:layout_marginTop 该控件离某元素上边缘的距离

 

 

5.3控件与控件之间的对齐方式

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

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

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

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

 

5.4控件与父控件的对齐方式

android:layout_alignParentBottom 值为true,则将该控件的底部和父控件的底部对齐

android:layout_alignParentLeft 值为true,则将该控件的左边与父控件的左边对齐

android:layout_alignParentRight 值为true,则将该控件的右边与父控件的右边对齐

android:layout_alignParentTop 值为true,则将控件的顶部与父控件的顶部对齐

 

 

5.5控件的方向

android:layout_centerHorizontal 值为true,控件将被至于水平方向的中央

android:layout_centerVertical 值为true,控件将被至于垂直方向的中央

android:layout_centerInParent 值为true,控件将被至于父控件水平方向和垂直方向的中央

 

 

**注意:

    1、不能产生循环依赖

       比如,不能将RelativeLayout的高设置成为WRAP_CONTENT的时候将子元素的高设置成为 ALIGN_PARENT_BOTTOM

    2、为了性能,采用相对布局代替嵌套线性布局

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面给出每种布局的一个简单实例: 1. 线性布局(LinearLayout) ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello" android:textSize="20sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="World" android:textSize="20sp" /> </LinearLayout> ``` 2. 相对布局(RelativeLayout) ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:textSize="20sp" /> <TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="World" android:textSize="20sp" android:layout_below="@id/text1" android:layout_alignParentRight="true" /> </RelativeLayout> ``` 3. 帧布局(FrameLayout) ```xml <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image1" android:scaleType="centerCrop" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello" android:textSize="20sp" android:layout_gravity="center" /> </FrameLayout> ``` 4. 表格布局(TableLayout) ```xml <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your name" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Age" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your age" /> </TableRow> </TableLayout> ``` 5. 网格布局(GridLayout) ```xml <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="3" android:columnCount="3"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="1" android:textSize="20sp" android:layout_row="0" android:layout_column="0" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="2" android:textSize="20sp" android:layout_row="0" android:layout_column="1" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="3" android:textSize="20sp" android:layout_row="0" android:layout_column="2" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="4" android:textSize="20sp" android:layout_row="1" android:layout_column="0" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="5" android:textSize="20sp" android:layout_row="1" android:layout_column="1" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="6" android:textSize="20sp" android:layout_row="1" android:layout_column="2" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="7" android:textSize="20sp" android:layout_row="2" android:layout_column="0" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="8" android:textSize="20sp" android:layout_row="2" android:layout_column="1" android:layout_columnWeight="1" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:text="9" android:textSize="20sp" android:layout_row="2" android:layout_column="2" android:layout_columnWeight="1" /> </GridLayout> ``` 以上是五种布局的简单实例,开发者可以根据具体需求进行修改和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值