(003) Andriod中的常用布局

本文详细介绍了Android中的几种布局,包括LinearLayout(线性布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)、TableLayout(表格布局,已过时)以及GridLayout(网格布局)。重点讲解了LinearLayout的方向属性、权重分配,以及RelativeLayout的相对位置设置,帮助开发者更好地理解和运用这些布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android  有五种布局:
 >Linearlayout  线性布局
 >RelativeLayout  相对布局
 
 >FrameLayout  帧布局
 >TableLayout   表格布局(过时)
 >AbsoluteLayout  绝对布局(过时) 

 4.0以后  新增了一个布局:GridLayout

 

1,View和ViewGroup
 >View  在UI界面  占矩形界面 
 >ViewGroup  不可见的 容器

 
2.LinearLayout线性布局********
 >概念:单一方向的布局  按水平方向或者垂直方向
 所有子元素 在水平方向或者垂直方向
 每一行或者每一列只能有一个元素

 >属性:
  >1,LinearLayout的方向属性   orientation
    android:orientation:方向属性   有俩个方向  horizontal(水平),vertical(垂直)
    android:orientation="horizontal"
    android:orientation="vertical"
    
    LinearLayout  默认是有方向的      水平方向
                >2,权重属性
      android:layout_weight="1" 权重属性   分配父控件的剩余空间
      剩余空间:父控件的宽或者高  减去  子控件 (所有子元素)的宽或者高 (*和方向有关)
      分配:子控件的宽或者高 = 原有的宽度或者高度  + 剩余空间(宽或者高)/子控件权重的和  *子控件的权重

  note :使用android:layout_weight时设置子控件的宽或者高为0dp(成比例进行分配)

  >3,   android:layout_gravity="bottom"  相对于父控件的位置
        android:layout_gravity属性和父控件的方向有关
        如果父控件是水平方向android:layout_gravity的自身的水平方向属性是无效的
        如果父控件是垂直方向 android:layout_gravity的自身的垂直方向属性是无效的

  >4,android:gravity="bottom|right"
         如果当前属性是在布局节点:子元素相对于自身的位置
         如果当前属性在控件中  (button ,TextView..)控制的是控件里的内容   (文字)
 
3.RelativeLayout相对布局*********
 >概念:根据控件的相对位置  进行布局 


 >属性:  16个
  >1,相对于父控件的位置
     
      1,居中位置
    android:layout_centerInParent="true"相对于父控件的中央位置
    android:layout_centerHorizontal="true"相对于父控件的水平居中
    android:layout_centerVertical="true"相对于父控件的垂直居中
   
   水平居中和垂直居中  可以重复使用   
      2,对齐位置
        android:layout_alignParentTop="true"   相对于父控件的  上对齐
        android:layout_alignParentBottom="true" 相对于父控件的  底边对齐
        android:layout_alignParentRight="true" 相对于父控件的  右对齐
        android:layout_alignParentLeft="true"  相对于父控件的  左对齐

  >2,相对兄弟控件的位置
   >1,相对兄弟元素的位置
       android:layout_above="@+id/tv1"   在指定兄弟控件的上边(顶部)
       android:layout_below="@+id/tv1"   在指定兄弟控件的下边(底边)
       android:layout_toLeftOf="@+id/tv1"在指定兄弟控件的左边
       android:layout_toRightOf="@+id/tv1"在指定兄弟控件的右边

   >2, 相对于兄弟控件的对齐方式 :
       android:layout_alignTop="@id/tv_money"  相对指定兄弟控件的顶部对齐
       android:layout_alignBottom="@id/tv_money"相对指定兄弟控件的底部对齐
       android:layout_alignLeft="@id/tv_money"  相对指定兄弟控件的左侧对齐
       android:layout_alignRight="@id/tv_money" 相对指定兄弟控件的右侧对齐
       android:layout_alignBaseline="@id/tv_money"相对指定兄弟控件的基准线对齐
     

4,Framelayout:帧布局  
 >概念:每个组件都是一帧   当前子组件会覆盖前一个组件

 >属性:
   android:layout_gravity="center"  控制 当前组件的位置
     
          android:foreground="@drawable/ic_launcher"放置在布局管理器所有组件之上
          android:foregroundGravity="top|right"  控制放置的位置  属性可以用"|" 叠加使用  属性不能互斥
   
5,GridLayout:网格布局

     android:orientation="vertical"  排列方式
   
     android:rowCount="3"  行的数量
    android:columnCount="3"列的数量
   
    位置是从0开始
     android:layout_row="0"    控件在第几行展示
     android:layout_column="0" 控件在第几列展示
    
    
    
     android:layout_columnSpan="2"列的跨度
     android:layout_rowSpan="2"  行的跨度
        android:layout_gravity="fill_horizontal"  填充所夸的行和列
        fill   水平和垂直方向都填充
        fill_horizontal  水平方向填充
        fill_vertical    垂直方向填充

 一人我饮酒醉:浪迹天涯只为早点回家

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值