Android的四个基本布局

在Android中有四种基本布局,可以放置很多控件的容器,按照一定的个一律调整控件的位置,从而编写出精美的界面

成都创新互联公司总部坐落于成都市区,致力网站建设服务有成都网站设计、做网站、网络营销策划、网页设计、网站维护、公众号搭建、成都小程序开发、软件开发等为企业提供一整套的信息化建设解决方案。创造真正意义上的网站建设,为互联网品牌在互动行销领域创造价值而不懈努力!

1)线性布局:LinearLayout

让我们来看一段代码

<LinearLayout xmlns:android="http://schemas,android.com/apk/res/android">android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:layout_gravity="top"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:layout_gravity="center_vertical"
android:text="Button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:layout_gravity="bottom"
android:text="Button3"/>
<LinearLayout/>

其中 orientation:vertical就是线性布局中的垂直布局

将其改为 orientation:horizantal就是线性布局中的水平布局

注释符后的android:l ayout_gravity似乎看起来和android:gravity很像,实际上 android:gravity是用来控制文字的, 而 android:l ayout_gravity是用来控制控件的, 需要注意的是 android:l ayout_gravity只能在垂直布局上用到,因为水平布局中的长度是不固定的,每添加一个就加长 还有一个也很重要的属性:android:layout_weight,这是用来比例控制控件大小 2)相对布局:RelativeLayout

<RelativeLayout xmlns:android="http://schemas,android.com/apk/res/android">
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button2"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button3"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button4"/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Button5"/>
<RelativeLayout/>

这里用到的是layout_alignParentTop,layout_alignParentBottm,layout_alignParentLeft,layout_alignParentRight和layout_centerInParent,他们的意思显而易见

除此以外还有,layout_above,layout_below,layout_toLeftof,layout_toRightof

而layout_alignLeft,layout_alignRight,layout_alignTop,layout_alignBottom,则是利用边缘对齐的方式控制控件 3)帧布局:FrameLayout 它相比于其他两个布局就简单多了,它没有方便的定位方式,所有的控件都会显示在左上角 4)百分比布局:PercentFrameLayout以及PercentRelativelayout 其中有:app:layout_heightPercent, app:layout_widthPercent 而在实际编程中,会发现一个问题,就是如果页面需要大量的重复控件,那么难道我们要一个个,一遍遍写吗? Android为我们提供了,引入布局的操作:<include layout="@layout/title"/>

分享标题:Android的四个基本布局
分享链接:https://www.cdcxhl.com/article38/ipcjsp.html

成都网站建设公司_创新互联,为您提供品牌网站设计微信小程序建站公司网站内链企业建站网站维护

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司