Android適配全攻略

前言

Android的適配是Android開發中的重要一環。一個良好的適配關乎着我們的用戶體驗,一直Android適配就是我們Android開發者所苦惱的事,也是需要重視的一件事。下面我就整理了一下有關適配的一些知識點。


概念

px: 像素,構成圖像的最小單位。

dpi: 屏幕像素密度,也就是屏幕1英寸長度上的像素點的個數,1英寸約等於2.54釐米。與屏幕尺寸和分辨率有關。具體計算公式爲:

假設,屏幕尺寸:5英寸, 分辨率爲 1280x720 , 那麼屏幕像素密度就等於1280的平方加上720的平方的和開根再除以5,即爲294dpi。

dp,dip: 密度無關像素。說明下:

在160dpi的屏幕上,1dp = 1px。 如果我們在寬160dpi的屏幕上畫一條長160px(160dp)的橫線,則剛好填滿寬。

如果在寬240dpi的屏幕上,我們畫一條160dp的橫線是否能夠填滿呢?答案是肯定的。因爲在240dpi的屏幕上,1dp=240/160px,即1dp=1.5px。那麼160dp=240px。剛好填滿。

所以稱之爲密度無關像素。

sp: 可以根據文字大小首選項進行放縮,相信看過TextView的源碼的朋友都知道,裏面的長度單位也是sp,因爲這是google推薦的字體大小單位。另外推薦使用12sp,14sp,18sp,22sp。不要小於12sp,以免用戶看不清,也不要使用奇數或是小數,在放縮時有可能造成精度丟失。


佈局

我相信,大家不會再使用絕對佈局吧,這個佈局已經違背適配的規範,所以不要使用。那麼我們就儘量使用線性佈局和相對佈局,在使用線性佈局時,使用weight屬性進行分配,關於weight,在文章後面會對其進行詳解。


圖片文件夾

相信不少朋友也看到我們的Android項目res目錄下有以下幾個文件夾描述

google爲我們創建的意義何在,也就是讓我們將不同分辨率的圖片放入不同的文件夾裏。每個文件夾所對應的屏幕像素密度爲:

mdpi :120dpi-160dpi
hdpi :160dpi-240dpi
xdpi :240dpi-320dpi
xxdpi :320dpi-480dpi
xxxdpi :480dpi-640dpi

也就是系統會根據屏幕dpi的不同,到對應的文件夾裏去使用對應的圖片。我們從自動創建的應用圖標也可以發現hdpi-xxxdpi文件夾裏的圖標大小依次爲:48x48px,72x72px,96x96px,144x144px,192x192px。

也就是我們在設計圖標遵循2:3:4:6:8的比例來設計就對了。

相信大家已經發現,爲什麼沒有ldpi呢?因爲ldpi這種設備已經太少了,它的屏幕像素密度太低。不過如果遇到,那麼也不用擔心。系統會自動將Hdpi裏的縮放至1/2進行適配。


weight

weight,這個屬性,我們再熟悉不過,那麼它的具體計算方法是怎樣的?你是否知道weightSum這個屬性?

好吧,下面我們一一揭曉,讓我們看看下面的代碼:

<LinearLayout 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"
    tools:context=".MainActivity"
    >

    <Button
        android:text="Button1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <Button
        android:text="Button2"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content" />
</LinearLayout>

相信大家已經知道顯示結果了,我就直接貼出來:
這裏寫圖片描述

那如果我將代碼改成這種,將如何顯示呢?

<LinearLayout 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"
    tools:context=".MainActivity"
    >

    <Button
        android:text="Button1"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <Button
        android:text="Button2"
        android:layout_width="match_parent"
        android:layout_weight="2"
        android:layout_height="wrap_content" />
</LinearLayout>

相信有些小夥伴已經猜到了,

這裏寫圖片描述

這裏我就直接貼出公式了(以寬度爲例),不明白的動動手。

weight 計算出的寬度=原有寬度+剩餘空間所佔百分比寬度

如果我只有一個控件,我想要它的寬度佔屏幕寬度的一半,怎麼辦?當然我們可以通過獲取屏幕寬度然後動態設置。不過,這裏有個更簡便的方法,那就是weightSum,看了下面的代碼你就懂了。

<LinearLayout 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"
    tools:context=".MainActivity"
    android:weightSum="2"
    >
    <Button
        android:text="Button"
        android:layout_width="0"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
</LinearLayout>

深入Android適配

這裏就直接貼出大神的幾篇blog:
Android 屏幕適配方案
Android 百分比佈局庫(percent-support-lib) 解析與擴展
Android 增強版百分比佈局庫 爲了適配而擴展
Android AutoLayout全新的適配方式 堪稱適配終結者

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章