常用的五大布局

常用的五大布局(线性布局,相对布局,帧布局,表格布局,绝对布局)

1,线性布局    LinearLayout

<LinearLyout
    android:layout_width="wrap_content"         //包裹内容
    android:layout_height="match_parent"       //填充父窗体
    android:orientation="vertical">                  //horizontal:  水平   vertical:垂直
        <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="我是按钮01"
        />
</LinearLyout> 

orientation:朝向:
weight: 权重    ,控制空间在父窗体里面显示的比例


计算器的原型图

2.相对布局    RelateLayout

<RelativeLayout
        android:layout_width=" wrap_content "
        android:layout_height=" wrap_content ">
             <Button
                    android:id="@+id/bt_middle"      //ID名字
                    //android:layouot_centerInParent="true"  //相对父控件居中   等同于下面两句
                    android:layout_centerHorizontal="true"  //控件水平居中
                    android:layout_centerVertical="true"   //控件垂直居中
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="中间"   />
              <Button
                android:layout_width="wrap_centnet"
                android:layout_height="wrap_centnet"
                android:layout_above="@+id/bt_middle"   //相对于某个控件的上方
                android:layout_centerHorizontal="true"   //水平居中
                android:text="↑"/>
</RelativeLayout>

        android:layout_above="@+id/bt_middle"            //相对于某个控件的上方
        android:layout_below="@id/bt_middle"             //相对于某个控件下方
        android:layout_toLeftOf="@id/bt_middle"         //相对于某个控件的左方
        android:layout_toRightOf="@id/bt_middle"      //相对于某个控件的右方

        android:layout_alignParentTop="true"         //与父窗体的上边角对齐
        android:layout_alignParentLeft="true"        //与父窗体的左边角的对齐
        android:layout_alignParentBottom="true"  //与父窗体的下边角对齐
        android:layout_alignParentRight="true"      //与父窗体的右边角对齐

3.帧布局    FrameLayout

类似>div    层叠布局

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >    
                <TextView
                         android:layout_width="300px"                                android:layout_height="300px"
                          android:layout_gravity="center"
                          android:backgroud="#ff00"   />
                 <TextView
                          android:layout_width="200px"                                android:layout_height="200px"                                                                                                     android:layout_gravity="center"                       
                          android:backgroud="#0ff"     />            
</FrameLayout>


运行结果为:

表格布局和绝对布局用的特别少.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容