ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Android入门教程 | UI布局之LinearLayout 线性布局

Android入门教程 | UI布局之LinearLayout 线性布局 Android有几种布局LinearLayout线性布局RelativeLayout相对布局FrameLayout帧布局TableLayout表格布局GridLayout网格布局AbsoluteLayout绝对布局LinearLayoutLinearLayout 又称作线性布局是一种非常常用的布局。LinearLayout 里面可以放置多个 view这里称为子view子项。 子 view 可以是TextViewButton或者是 LinearLayoutRelativeLayout 等等。 它们将会按顺序依次排布为一列或一行。 接下来介绍一些在 xml 中的设置。先在 xml 中放一个 LinearLayout。LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationvertical/LinearLayout下面介绍一些属性。竖直排布与水平排布通过设置 orientation 来确定水平或竖直排布子 view。 可选值有 vertical 和 horizontal。竖直排布设置 orientation 为 vertical。android:orientationvertical水平排布设置 orientation 为 horizontal。android:orientationhorizontal排布方式 gravity决定子 view 的排布方式。gravity有“重力”的意思我们引申为子view会向哪个方向靠拢。gravity有几个选项可以选择我们常用的有startendleftrighttopbottom。例如:LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:gravitystartandroid:orientationvertical/LinearLayout下面是 gravity 的选项。我们会把 LinearLayout 叫做“父view”或者“容器”。常量定义值描述top30把view推到容器里的顶部。不会改变view的尺寸。bottom50把view推到容器的底部。不会改变view的尺寸。center11子view水平与竖直都居中。不会改变view的尺寸。center_horizontal1子view水平居中。不会改变view的尺寸。center_vertical10子view垂直居中。不会改变view的尺寸。start800003把view推到容器里最开始的地方。不会改变view的尺寸。end800005把子view放到容器的最尾部。不改变view的尺寸。left3子view从容器的左边开始排布。不会改变view的尺寸。right5子view从容器的右边开始排布。不会改变view的尺寸。start 和 leftend 和 right 并不一定是同样的效果。 对于 RTLright to left类型的手机比如某些阿拉伯文的系统。start 是从右到左的。 我们日常生活中很少见到RTL一般都是 LTR。但还是建议多用 start 而不是 left。gravity可以同时设置多个值用或符号|来连接。比如android:gravityend|center_vertical。LinearLayoutandroid:layout_widthmatch_parentandroid:layout_height60dpandroid:gravityend|center_verticalandroid:orientationvertical/LinearLayout子 view 的 layout_gravitylayout_gravity 看起来和 gravity 有些相似。android:gravity控制自己内部的子元素。android:layout_gravity是告诉父元素自己的位置。取值范围和gravity是一样的。代表的含义也相似。LinearLayoutandroid:layout_widthmatch_parentandroid:layout_height100dpandroid:orientationhorizontalTextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:background#90CAF9android:textnone/TextViewandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_gravitycenterandroid:background#9FA8DAandroid:textcenter//LinearLayout分割占比layout_weight可以在设置子 view 的 layout_weight来确定空间占比。 设置layout_weight的时候一般要设置layout_width0dp。LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:background#FFCC80android:orientationhorizontalTextViewandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:layout_weight1android:background#eaeaeaandroid:gravitycenterandroid:text1/TextViewandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:layout_weight2android:text2/TextViewandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:layout_weight1android:background#eaeaeaandroid:text1/TextViewandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:layout_weight3android:background#BEC0D1android:text3//LinearLayout分割占比之和 weightSumandroid:weightSum定义子 view 的 weight 之和的最大值。如果不直接指定它会是所有子 view 的layout_weight之和。 如果想给单独的一个子 view 一半的空间占比可以设置子 view 的 layout_weight 为0.5并且设置 LinearLayout 的 weightSum 为1.0。取值可以是浮点数比如9.3。LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop10dpandroid:background#4DB6ACandroid:weightSum9.3android:orientationhorizontalTextViewandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:layout_weight4.6android:background#eaeaeaandroid:gravitycenterandroid:text4.6/TextViewandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:background#7986CBandroid:layout_weight2.5android:text2.5//LinearLayout分割线 divider设置 divider 与 showDividers 属性。pre id__code_8stylebox-sizing: inherit; color: var(--md-code-fg-color); font-feature-settings: quot;kernquot;; font-family: quot;Roboto Monoquot;, SFMono-Regular, Consolas, Menlo, monospace; direction: ltr; position: relative; margin: 1em 0px; line-height: 1.4;android:dividerdrawable/divider_linear_1android:showDividersmiddle直接给divider设置颜色是无效的。在drawable目录里新建一个xml叫做divider_linear_1.xml。?xml version1.0encodingutf-8?shape xmlns:androidhttp://schemas.android.com/apk/res/androidsolid android:color#7986CB/sizeandroid:width1dpandroid:height1dp//shapesize 必须指定否则当做 divider 来用时会显示不出来。LinearLayout 设置 divider。LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:background#eaeaeaandroid:dividerdrawable/divider_linear_1android:orientationverticalandroid:showDividersmiddle....LinearLayoutandroid:layout_widthmatch_parentandroid:layout_height100dpandroid:layout_marginTop20dpandroid:background#FFD9D9android:dividerdrawable/divider_linear_1android:orientationhorizontalandroid:showDividersmiddle...showDividers有几种可选middle 中间的分割线beginning 开始的分割线end 结束的分割线none 没有分割线最后分享一份【腾讯技术团队出品】Android零基础入门到精通Android Studio安装教程全套安卓基础教程Android编程入门教程Java语言基础从入门到熟悉Kotlin语言基础从入门到熟悉Android 技术栈从入门到熟悉Android Jetpack 全家桶全面学习对于新手来说可能安装Android Studio存在一定困难你可以看着以下视频一步步的跟着学习安装运行Android Studio 安装教程有了Java阶段的学习这一阶段建议以视频学习为主辅以图书查漏补缺。如果以图书为主可以根据图书讲解敲代码辅以教学视频查漏补缺。遇到问题可以去百度入门的问题一般会有很多人遇到并且给出比较好的解答。需要掌握基本知识点比如四大组件如何使用、如何创建Service、如何进行布局、简单的自定义View、动画、网络通信等常见技术。全套零基础教程已经为你们准备好了需要的可以添加下方二维码免费领取全套安卓基础教程
返回列表