iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Kotlin LinearLayout与RelativeLayout布局使用详解
  • 435
分享到

Kotlin LinearLayout与RelativeLayout布局使用详解

Kotlin LinearLayoutKotlin RelativeLayout 2022-12-08 20:12:39 435人浏览 泡泡鱼
摘要

目录线性布局LinearLayout相对布局RelativeLayout小结安卓的开发从布局开始。 安卓的界面编写也是使用xml进行布局的,一般如果熟悉了html界面的布局,那么很容

安卓的开发从布局开始。

安卓的界面编写也是使用xml进行布局的,一般如果熟悉了html界面的布局,那么很容易就能够理解安卓有关的布局了,这里介绍两个比较重要的布局方式:线性布局(LinearLayout)和相对布局(RelativeLayout)。

新建的功能布局,一般是一个界面对应一个xml文件,main界面的xmlactivity_main.xml 中。

线性布局LinearLayout

根据名字我们就很清楚,线性布局的意思了,相当于html中的div层,两种布局方向:

vertical 下的布局方式:

horizontal 下的布局方式:

vertical 布局代码:

<LinearLayout
    Android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#00aaff" >
    <Button
        android:id = "@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button" />
    <Button
        android:id = "@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button" />
</LinearLayout>

horizontal 下的布局代码:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#A6A7AF" >
        <Button
            android:id = "@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="Button" />
        <Button
            android:id = "@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:text="Button" />
    </LinearLayout>

有几个属性需要熟悉一下:

  • wrap_content 为按照控件内容的大小进行调整
  • layout_marginLeft 为控件左边的偏移,其他的一次类推
  • layout_gravity 可以用来进行控件居中显示
  • layout_weight 控件在horizontal模式下占到的比率

相对布局RelativeLayout

相对布局 主要两种相当模式,一种是父控件,一种是相对兄弟控件。

布局代码如下:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#9C27B0" >
        <Button
            android:id = "@+id/btn5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:text="Button5" />
        <Button
            android:id = "@+id/btn6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Button6" />
        <Button
            android:id = "@+id/btn7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_below="@+id/btn6"
            android:text="Button7" />
    </RelativeLayout>

几个重要的布局:

  • layout_alignParentxxxx 相对于父类的情况
  • layout_to 相对于兄弟的情况

项目GitHub的地址在这里。

小结

布局的方式比较多,但是这两个种布局方式是最重要的,也可以这么说掌握了这两种以后,其他的就可以依次类推,只要知道里面的属性基本上就容易上手了。

到此这篇关于Kotlin LinearLayout与RelativeLayout布局使用详解的文章就介绍到这了,更多相关Kotlin LinearLayout与RelativeLayout内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Kotlin LinearLayout与RelativeLayout布局使用详解

本文链接: https://www.lsjlt.com/news/174361.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作