Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 版面配置與 CSS Flex 布局屬性對照表

編輯:關於 Android 程式設計

一、前言

身為 Android 開發者,用 XML 寫 UI 實在太方便了。近來學習 Weex 時需要用 CSS 來布局,學完之後發現 CSS 的 Flex 布局同樣相當順手。在 CSS 中使用 Flex 需要宣告 display 屬性(Weex 預設就是 flex):

.box {
  display: -webkit-flex; /* Safari */
  display: flex;
}

二、用 CSS 寫版面配置

2.1 線性布局

對 Android 開發者而言,線性布局用 LinearLayout;而在 CSS 裡,不論線性還是相對,一律用 div 標籤——div 的角色有點像 Android 的 ViewGroup。

方向

垂直方向(由上而下):

android:orientation="vertical"
flex-direction: column;

水平方向(由左而右):

android:orientation="horizontal"
flex-direction: row;

例如寫一個左右排列的布局,效果如下:

權重

Android 中 LinearLayout 可設定 android:weightSum,子元素再設 android:layout_weight,用來做等分效果:

<LinearLayout
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="100px"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView
        android:background="#ff0000"
        android:gravity="center"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="1"
        android:textColor="#000000" />

    <TextView
        android:background="#0000ff"
        android:gravity="center"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:text="2"
        android:textColor="#000000" />
</LinearLayout>

Flex 中則直接使用 flex 屬性即可,效果如下:

這裡與 Android 類似,flex 的優先級高於 width。

2.2 相對布局

Android 用 RelativeLayout 實作相對布局。一般來說,相對布局能做到的樣式,大多可以用多層嵌套的線性布局替代,例如 toLeftOf、toRightOf、below、above,分別對應某元素的左、右、下、上方。

但也有例外,比如覆蓋效果:

一個藍色方塊疊在紅色方塊之上,位於右下角,距右與距下各 40 像素。用 RelativeLayout 寫起來非常直觀,主要用到的屬性有:

// 與父布局的相對關係
android:layout_alignParentLeft
android:layout_alignParentTop
android:layout_alignParentRight
android:layout_alignParentBottom
// 外邊距
android:layout_marginLeft
android:layout_marginTop
android:layout_marginRight
android:layout_marginBottom
<RelativeLayout
    android:layout_width="160dp"
    android:layout_height="160dp"
    android:background="#ff0000">

    <View
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="#00ff00"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="40dp"
        android:layout_marginRight="40dp">

        <View
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="#0000ff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="20dp"
            android:layout_marginRight="20dp" />
    </View>
</RelativeLayout>

CSS 則要引入這幾個屬性:

position: relative 或 absolute
top, left, bottom, right

子元素使用 absolute 絕對定位,再用 bottom 與 right 把它定位到上一層中最近一個 position 為 relative 的 div 裡。bottom 指距底部的距離,right 指距右側的距離;若改用 top 與 left,則定位上緣與左緣。

補充一點:若 left 與 right 同時使用,要看有沒有指定 width。沒指定時兩者同時生效,寬度會依 left 與 right 自動推算;一旦指定了寬度,則只有 left 與 width 生效、right 被忽略,應盡量避免這種寫法。

2.3 居中問題

Android 中線性布局與相對布局的居中寫法不同。LinearLayout 用 android:gravity 與 android:layout_gravity 決定;RelativeLayout 則用:

水平居中:android:layout_centerHorizontal
垂直居中:android:layout_centerVertical
全居中:android:layout_centerInParent

Flex 中對應為:

水平居中:justify-content: center;
垂直居中:align-items: center;
全居中:同時使用 justify-content: center; 與 align-items: center;

此外這兩個屬性還有其他可選值:

.box { justify-content: flex-start | flex-end | center | space-between | space-around; }
.box { align-items: flex-start | flex-end | center | baseline | stretch; }

各自的具體含義可查閱文件,此處不再贅述。

2.4 邊距

要讓內距與外距的含義和 Android 一致,需要在 CSS 樣式中加上:

box-sizing: border-box;

如此一來,為元素指定的任何內距與邊框都會在已設定的寬高之內繪製,內容的實際寬高需從設定值中扣除邊框與內距後才得到——這就與 Android 的 padding 行為一致了。

// Android 內距
android:padding / paddingLeft / paddingRight / paddingTop / paddingBottom
// Android 外距
android:margin / layout_marginLeft / layout_marginTop / layout_marginRight / layout_marginBottom

/* CSS 內距 */
padding / padding-top / padding-right / padding-bottom / padding-left
/* CSS 外距 */
margin / margin-top / margin-right / margin-bottom / margin-left

Android 的邊框通常得靠 shape 才有,CSS 則簡單得多:

border / border-width / border-style / border-color

2.5 圓角弧度

Android 要寫圓角 shape 也不難,用 shape 即可:

radius / topLeftRadius / topRightRadius / bottomLeftRadius / bottomRightRadius

CSS 與之類似:

border-radius
border-top-left-radius
border-top-right-radius
border-bottom-right-radius
border-bottom-left-radius

注意:Android 只能對背景設定 shape,控制項本身的內容不會改變;而 CSS 的 border-radius 既能改背景,也能改控制項內容本身。比方說要顯示一張圓形圖片,在 Android 上怎麼設 ImageView 屬性都做不到,只能另寫一個 CircleImageView 之類的圓角控制項;CSS 直接用 border-radius 就搞定了。

2.6 其他屬性

常見的 Android 屬性對照如下:

android:background   // 背景
android:text         // 文本內容
android:textColor    // 文本顏色
android:textSize     // 文本大小
android:textStyle    // 文本風格
android:singleLine   // 單行顯示
android:maxLines     // 多行顯示
android:hint         // 預設提示文字
android:scaleType    // 圖片縮放方式
android:ellipsize    // 溢出處理

背景色

background-color: #dddddd;
background-color: rgba(0, 0, 0, 0.5);

其中 R 為紅色值、G 為綠色值、B 為藍色值,皆可為正整數或百分比;A 為透明度,取值介於 0 到 1 之間。

文本內容

直接放進標籤內部即可。

文本顏色與大小

color: #ff0000;
font-size: 24px;

風格

.normal  { font-style: normal; }   /* 預設,正常 */
.italic  { font-style: italic; }   /* 斜體 */
.oblique { font-style: oblique; }  /* 傾斜 */
.normal  { font-weight: normal; }  /* 預設,正常 */
.thick   { font-weight: bold; }    /* 加粗 */
.thicker { font-weight: 900; }     /* 加粗 */

行數限制

單行限制、多餘部分顯示省略號:

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

部分瀏覽器還需要搭配 width 限制。

限制多行(以 3 行為例):

display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;

Weex 的 text 元件另外提供了 lines 屬性來限制行數:

.togetherTitle {
  width: 500px;
  text-overflow: ellipsis;
  margin-top: 10px;
  font-size: 28px;
  color: #4a4a4a;
  lines: 1;   /* Weex 屬性,限制為 1 行 */
}
.togetherContent {
  text-overflow: ellipsis;
  height: 100px;
  width: 500px;
  margin-top: 5px;
  font-size: 26px;
  color: #9f9f9f;
  lines: 2;   /* Weex 屬性,限制為 2 行 */
}

文本對齊

Android 中 TextView 的 android:gravity(left、center、right),在 CSS 中對應為:

text-align: center;

left 靠左(瀏覽器預設)、right 靠右、center 置中、justify 兩端對齊。

圖片 scaleType

Android 中 ImageView 的 scaleType 常用 centerCrop 與 fitXY。CSS 要實作 fitXY 很簡單,用 img 標籤並指定寬高即可;要實作 centerCrop 則需三個樣式配合:

background-image: url('xxxx');
background-size: 80px 120px;
background-position: center;

關鍵屬性就是 background-position,設為 center 即為置中,它還有 left、right 等其他取值。Weex 則使用 image 元件的 resize 屬性。

2.7 可見性控制

Android 用 visibility 屬性控制可見性:

android:visibility="gone | visible | invisible"

gone 是既不可見也不佔位;visible 是既可見又佔位;invisible 是不可見但佔位。

CSS 中「可見並佔位」是預設狀態,另兩種則分別對應:

display: none;    /* 對應 gone:不可見且不佔位 */
visibility: hidden;  /* 對應 invisible:不可見但佔位 */

而在 React 這類框架中,我們通常直接靠資料源來控制 div 的顯示與否。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved