Day5 Android - Layout版面(下)

继昨天讲了的ConstraintLayout,今天要来介绍自己也常用的另外两个布局,LinearLayout以及RelativeLayout,首先从LinearLayout先开始。


LinearLayout

LinearLayout(线性布局),严格来说又可分成两种布局方向,一种是水平方向的(vertical)以及垂直方向的(horizontal),首先先附上程序:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="100dp"
        android:padding="20dp"/>

</LinearLayout>

在最外层的layout,可以看到他的布局方向(orientation)是垂直方向(horizontal),若orientation这个属性改成vertical则是水平方向的,接着先来讲一些常用到的属性。

常用属性

其中里面也蛮多通用於其他布局的属性,主要有:

  • android:id > 可用来辨识元件的标签id。(="@+id/????"),?的部分可以用来设定你这个元件的id,自定的。
  • android:layout_width及height则分别是他的宽、高。(="wrap_content":依照内容物件的大小去更动宽或高,="match_parent":填满上层元件的宽或高)
  • android:layout_weight > 权重,值越小则比例越小,比例为1(="1"),如果现在有两个元件的宽要七三分,就是一个设(="0.7")、一个设(="0.3"),大约是这样。

(元件内外部距离)

  • android:padding > 元件内边的距离(="20dp"则内边界为20dp的距离)。
  • android:layout_margin > 元件外边的距离。(="20dp"则外边界的距离为20dp)

(元素位置控制)

  • android:gravity="" > 可用於控制元件里面元素的方向(文字等,如center、left、center|vertical等)。
  • android:layout_gravity="" > 可用於控制元件在父层里的位置(center、center|vertical...等)。
  • android:paddingTop > 此元件内容物件与元件顶边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:paddingBottom > 此元件与元件底边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:paddingLeft > 此元件内容物件与元件左边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:paddingRight > 此元件内容物件与元件右边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:layout_marginTop > 此元件与父层顶边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:layout_marginBottom > 此元件与父层底边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:layout_marginLeft > 此元件与父层左边界拉开的距离(="20dp"则拉开20dp的距离)
  • android:layout_marginRight > 此元件与父层右边界拉开的距离(="20dp"则拉开20dp的距离)
    更多属性请参阅:Android Developers/LinearLayout

RelativeLayout

RelativeLayout(相对布局),主要可透过相对位置(相对布局、元件)来调整、设定其元件的位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="100dp"
        android:layout_marginTop="100dp"
        android:text="Hello World!"
        android:padding="50dp"/>

</RelativeLayout>

常用属性

  • android:id > 可用来辨识元件的标签id。
  • android:layout_width及height则分别是他的宽、高。

(与元件对齐):(="@+id/????")?为你要对齐的元件的id。

  • android:layout_above > 将此元件的底部边缘置於指定元件的顶部边缘(上面)。
  • android:layout_below > 将此元件的顶部边缘置於指定元件的底部边缘(下面)。
  • android:layout_toLeftOf > 将此元件的右边缘置於指定元件的左边缘(左边)。
  • android:layout_toRightOf > 将此元件的左边缘置於指定元件的右边缘(右边)。

(与父层对齐):(="true")

  • android:layout_alignParentTop > 将此元件的顶部边缘与父层的顶部边缘对齐。
  • android:layout_alignParentBottom > 将此元件的底部边缘与父层的底部边缘对齐。
  • android:layout_alignParent > 将此元件的左边缘与父层的左边缘对齐。
  • android:layout_alignParentEnd > 将此元件的右边缘与父层的右边缘对齐。

(与对应父层方向拉开的距离 margin):(="????dp")你要拉开?dp的距离,或者你想用sp、dpi等都可以。

  • android:layout_marginTop > 此元件与顶部拉开多少距离。
  • android:layout_marginBottom > 此元件与底部拉开多少距离。
  • android:layout_marginLeft > 此元件与左边拉开多少距离。
  • android:layout_marginRight > 此元件与右边拉开多少距离。

更多属性请参阅:Android Developers/RelativeLayout


<<:  [13th][Day10] waiting group

>>:  失控玩家 又名 脱稿玩家 free guy

Day030-与Vue相遇--铁人赛回顾

回顾30天,也让我回忆了今年从金融业被抓去做开发工程师的日子。这30天中,讲述了Vue的基本介绍、性...

DAY22 时刻表选取组别功能实现

if event.postback.data[:9] == "Schedule:"...

Vue 动态组件

tags: Vuejs 动态组件 ✐ 动态组件可以帮助我们动态切换组件,例如在网页的多标签介面中常见...

[Day5] - Django 介绍

现在开始要开始介绍我们使用的工具了,工欲善其事必先利其器,这一篇首先带大家来了解一下我们要使用的Dj...

[Day15] LocalStorage and Event Delegation

[Day15] LocalStorage and Event Delegation 需要用到的技巧与...