查看:http://lib.csdn.net/article/android/57912
http://www.jianshu.com/p/6319e43572fa
1)應用中的App Icon及【首頁】的圖標:
drawable-hdpi: 72*72px
drawable-mdpi: 48*48px
drawable-xdpi: 96*96px
android啓動圖標
drawable-mdpi: 48*48px
drawable-hdpi: 72*72px
drawable-xdpi: 96*96px
drawable-xxdpi: 144*144px
2)常用的按鈕:
drawable-mdpi: 高 32px
drawable-hdpi: 高 48px
drawable-xdpi: 高64px
3)【更多】中的圖標:
drawable-mdpi: 32px * 32px
drawable-hdpi: 48px*48px
drawable-xdpi: 64px*64px
android背景圖:
drawable-ldpi : 320×240px
drawable-mdpi: 480*320px
drawable-hdpi: 800*460px
drawable-xdpi:1280*720px
drawable-xxhdpi : 1920*1080px
drawable-xxxhdpi :3840×2160px
1. 什麼是屏幕尺寸、屏幕分辨率、屏幕像素密度?
屏幕尺寸是指屏幕對角線的長度。單位是英寸,1英寸=2.54釐米
屏幕分辨率是指在橫縱向上的像素點數,單位是px,1px=1像素點,一般是縱向像素橫向像素,如1280×720
屏幕像素密度是指每英寸上的像素點數,單位是dpi,即“dot per inch”的縮寫,像素密度和屏幕尺寸和屏幕分辨率有關
例如:計算Nexus5的屏幕像素密度:
屏幕尺寸:4.95inch、分辨率:1920×1080,屏幕像素密度:445
2. 什麼是dp、dip、dpi、sp、px?之間的關係是什麼?
dip:Density Independent Pixels(密度無關像素)的縮寫。以160dpi爲基準,1dp=1px
dp:同dip
dpi:屏幕像素密度的單位,“dot per inch”的縮寫
px:像素,物理上的絕對單位
sp:Scale-Independent Pixels的縮寫,可以根據文字大小首選項自動進行縮放。Google推薦我們使用12sp以上的大小,通常可以使用12sp,14sp,18sp,22sp,最好不要使用奇數和小數。
說明:如果A設備的參數爲480×320,160dpi,B設置的參數爲800×480,240dpi。我們要畫出一條和屏幕寬度一樣長的直線,如果使用px作爲單位,必須在A設備上設置爲320px,在B設備上設置480px。但是如果我們使用dp作爲單位,由於以160dpi爲基準,1dp=1px,所以A設備上設置爲320dp就等於屏幕寬度(320px),在B設備上設置爲320dp就等於320×(240/160)=480px,即B設備的屏幕寬度。這樣,使用dp作爲單位就可以實現簡單的屏幕適配。這知識一種巧合,也有B設備的像素密度不是這樣剛剛好的,就需要我們運用別的屏幕適配技術。
3. 什麼是mdpi、hdpi、xdpi、xxdpi、xxxdpi?如何計算和區分?
用於區分不同的像素密度。
在Google官方開發文檔中,說明了 mdpi:hdpi:xhdpi:xxhdpi:xxxhdpi=2:3:4:6:8 的尺寸比例進行縮放。例如,一個圖標的大小爲48×48dp,表示在mdpi上,實際大小爲48×48px,在hdpi像素密度上,實際尺寸爲mdpi上的1.5倍,即72×72px,以此類推。
二. 解決方案-支持各種屏幕尺寸
我們可以通過以下幾種方式來支持各種屏幕尺寸:
1. 使用wrap_content、math_parent、weight
wrap_content:根據控件的內容設置控件的尺寸
math_parent:根據父控件的尺寸大小設置控件的尺寸
weight:權重,在線性佈局中可以使用weight屬性設置控件所佔的比
例如,我們要實現下圖所顯示的效果:當屏幕尺寸改變時,new reader控件兩邊的控件大小不變,new reader控件會佔完剩餘的空間。
實現:通過給new reader控件設置屬性:
android:layout_width="wrap_content",android:layout_weight="1",兩邊兩個控件爲固定大小,android:layout_weight="0"
具體佈局文件如下:
<?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:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="0"
android:background="#028330"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="new
reader"
android:textSize="22sp"
android:layout_weight="1"/>
<TextView
android:layout_width="160dp"
android:layout_height="80dp"
android:text="Politics"
android:textSize="18sp"
android:layout_weight="0"
android:background="#028330"/>
</LinearLayout>
補充說明:關於android:layout_weight屬性
公式:所佔寬度=原來寬度+剩餘空間所佔百分比的寬度
一般情況,我們都是設置要進行比例分配的方向的寬度爲0dp,然後再用權重進行分配。
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2" />
效果爲:
設屏幕寬度爲L,
根據公式,
button1寬度=0+L×1/(1+2)=1/3L
button2寬度=0+L×2/(1+2)=2/3L
但如果設置爲match_parent:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button2" />
效果爲:
button1寬度=L+(L-2L)×1/3=2/3L
button2寬度=L+(L-2L)×2/3=1/3L
當然,還有其他的方式,都可以運用此公式進行計算。
在實際開發中,我們一般使用0dp的方式,而不使用其他方式。
2. 使用相對佈局,禁用絕對佈局
簡單的佈局一般都使用線性佈局,而略微複雜點的佈局,我們使用相對佈局,大多數時候,我們都是使用這兩種佈局的嵌套。
我們使用相對佈局的原因是,相對佈局能在各種尺寸的屏幕上保持控件間的相對位置。
3. 使用限定符
使用尺寸限定符
當我們要在大屏幕上顯示不同的佈局,就要使用large限定符。例如,在寬的屏幕左邊顯示列表右邊顯示列表項的詳細信息,在一般寬度的屏幕只顯示列表,不顯示列表項的詳細信息,我們就可以使用large限定符。
res/layout/main.xml 單面板:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 列表 -->
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="match_parent" />
</LinearLayout>
res/layout-large/main.xml 雙面板:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<!-- 列表 -->
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="400dp"
android:layout_marginRight="10dp"/>
<!-- 列表項的詳細信息 -->
<fragment android:id="@+id/article"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.ArticleFragment"
android:layout_width="fill_parent" />
</LinearLayout>
如果這個程序運行在屏幕尺寸大於7inch的設備上,系統就會加載res/layout-large/main.xml 而不是res/layout/main.xml,在小於7inch的設備上就會加載res/layout/main.xml。
需要注意的是,這種通過large限定符分辨屏幕尺寸的方法,適用於android3.2之前。在android3.2之後,爲了更精確地分辨屏幕尺寸大小,Google推出了最小寬度限定符。
使用最小寬度限定符
最小寬度限定符的使用和large基本一致,只是使用了具體的寬度限定。
res/layout/main.xml,單面板(默認)佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="match_parent" />
</LinearLayout>
res/layout-sw600dp/main.xml,雙面板佈局: Small Width 最小寬度
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<fragment android:id="@+id/headlines"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.HeadlinesFragment"
android:layout_width="400dp"
android:layout_marginRight="10dp"/>
<fragment android:id="@+id/article"
android:layout_height="fill_parent"
android:name="com.example.android.newsreader.ArticleFragment"
android:layout_width="fill_parent" />
</LinearLayout>
這種方式是不區分屏幕方向的。這種最小寬度限定符適用於android3.2之後,所以如果要適配android全部的版本,就要使用large限定符和sw600dp文件同時存在於項目res目錄下。
這就要求我們維護兩個相同功能的文件。爲了避免繁瑣操作,我們就要使用佈局別名。
使用佈局別名
res/layout/main.xml
: 單面板佈局
res/layout-large/main.xml
: 多面板佈局
res/layout-sw600dp/main.xml
: 多面板佈局
由於後兩個文具文件一樣,我們可以用以下兩個文件代替上面三個佈局文件:
res/layout/main.xml
單面板佈局
res/layout/main_twopanes.xml
雙面板佈局
然後在res下建立
res/values/layout.xml
、
res/values-large/layout.xml
、
res/values-sw600dp/layout.xml
三個文件。
默認佈局
res/values/layout.xml
:
<resources>
<item name="main" type="layout">@layout/main</item>
</resources>
Android3.2之前的平板佈局
res/values-large/layout.xml
:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
Android3.2之後的平板佈局
res/values-sw600dp/layout.xml
:
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
這樣就有了main
爲別名的佈局。
在activity中setContentView(R.layout.main);
這樣,程序在運行時,就會檢測手機的屏幕大小,如果是平板設備就會加載res/layout/main_twopanes.xml
,如果是手機設備,就會加載res/layout/main.xml
。我們就解決了只使用一個佈局文件來適配android3.2前後的所有平板設備。
使用屏幕方向限定符
如果我們要求給橫屏、豎屏顯示的佈局不一樣。就可以使用屏幕方向限定符來實現。
例如,要在平板上實現橫豎屏顯示不用的佈局,可以用以下方式實現。
res/values-sw600dp-land/layouts.xml
:橫屏
<resources>
<item name="main" type="layout">@layout/main_twopanes</item>
</resources>
res/values-sw600dp-port/layouts.xml
:豎屏
<resources>
<item name="main" type="layout">@layout/main</item>
</resources>
4. 使用自動拉伸位圖
自動拉伸位圖,即android下特有的.9.png
圖片格式。
當我們需要使圖片在拉伸後還能保持一定的顯示效果,比如,不能使圖片中的重要像素拉伸,不能使內容區域受到拉伸的影響,我們就可以使用.9.png
圖來實現。
要使用.9.png
,必須先得創建.9.png
圖片,androidSDK給我們提供了的工具就包含.9.png
文件的創建和修改工具。雙擊SDK安裝目錄 oolsdraw9patch.bat
,就會打開下圖所示的窗口。
**直接把圖片拖進去,選擇Filesave.9.png,即可保存爲.9.png圖片。
不過,這和原圖沒有什麼區別,我們要製作成自動拉伸的圖片,還需要進行簡單的處理。**
在左邊和上邊點下一個像素或多個像素點,是選擇了拉伸的像素,即把選擇的像素點進行拉伸。選擇的像素點越多,拉伸的程度也越大。
右邊和下邊是選擇內容區域,在右邊和下邊畫上直線,交叉的區域就是內容區域。
什麼事內容區域呢?
比如,我們給Button設置了一個.9.png
作爲背景,還要設置其android:text屬性,設置的text所佔的位置就是內容區域。
我們必須保證內容區域包含text文本,纔會正常顯示出text文本的內容。
下面是一個例子:
上圖是我們製作的.9.png
,設置好了左上拉伸像素點和內容區域。
Button屬性設置:
<Button
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/button"
android:text="Button" />
如果我們選擇的內容區域偏差太大,可能就不會顯示出text值BUTTON
。