Android自適應不同螢幕幾種方法,android自適應

來源:互聯網
上載者:User

Android自適應不同螢幕幾種方法,android自適應
    由於Android裝置的螢幕尺寸、解析度差別非常大,如果希望我們的應用能夠在不同螢幕尺寸或解析度的Android裝置上運行,即更換Android裝置後介面和字型不會因此變得混亂,則需要考慮螢幕的自適應性問題。相關概念:(1)螢幕尺寸(Screen size):即指螢幕的對角線長度,螢幕尺寸可分為small(小螢幕)、normal(中等螢幕)、large(大螢幕)、xlarge(超大螢幕);(2)解析度(dp):即整個螢幕有多少個像素點組成,可分為ldpi(低解析度)、mdpi(中等解析度)、hdpi(高解析度)、xhdpi(超高解析度);    >xlarge螢幕尺寸解析度至少需要960dp*720dp    >large螢幕尺寸解析度至少需要640dp*480dp    >normal螢幕尺寸解析度至少需要470dp*320dp    >small螢幕尺寸解析度至少需要426dp*320dp(3)像素密度(dpi):每英寸螢幕含有的像素點個數 (android 也按照像素密度分了四個等級,分別是low, medium, high, 和 extra high);(4)方向(Orientation) :分水平和垂直,如果應用做的好的話,這兩個方向都要考慮;    android 對不同尺寸不同像素密度等級劃分: 一、使用layout_weight屬性方法    目前最為推薦的Android多螢幕自適應解決方案。該屬性的作用是決定控制項在其父布局中的顯示權重,一般用於線性布局中。layout_weight屬性值越小,則對應的layout_width或layout_height的優先順序就越高,一般橫向布局中,決定的是layout_width的優先順序;縱向布局中,決定的是layout_height的優先順序。目前有兩種方法:(1)傳統使用方法:將layout_width和layout_height設定為fill_parent,通過layout_weight屬性控制控制項的顯示比例,當layout_weight越小,控制項顯示比例就越大。但是,如果布局中控制項較多且顯示比例不相同時,傳統使用layout_weight屬性的方法將比較麻煩。(2)0px設值法:即使layout_width="0dp"或layout_height="0dp",再結合layout_weight屬性正比例控制控制項的顯示,該方法有效解決了當前Android開發中片段化問題之一。源碼舉例:1.res/values/style_layout.xml功能:將控制項的layout_width和layout_height兩個屬性封裝成4個style<?xml version="1.0" encoding="utf-8"?><resources>  <!-- 全螢幕展開-->  <style name="layout_full">      <item name="android:layout_width">fill_parent</item>      <item name="android:layout_height">fill_parent</item>    </style><!-- 固定自身大小-->  <style name="layout_wrap">      <item name="android:layout_width">wrap_content</item>      <item name="android:layout_height">wrap_content</item>    </style><!-- 橫向分布-->  <style name="layout_horizontal" parent="layout_full">      <item name="android:layout_width">0px</item>    </style>   <!-- 縱向分布-->  <style name="layout_vertical" parent="layout_full">      <item name="android:layout_height">0px</item>    </style>       </resources>2.res/layout/weight_layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        style="@style/layout_full"        android:orientation="vertical">        <LinearLayout                style="@style/layout_vertical"                android:layout_weight="1"                android:orientation="horizontal">                 <View                         style="@style/layout_horizontal"                         android:background="#aa0000"                         android:layout_weight="1"/>                 <View                         style="@style/layout_horizontal"                         android:background="#00aa00"                         android:layout_weight="4"/>                 <View                         style="@style/layout_horizontal"                         android:background="#0000aa"                         android:layout_weight="3"/>                 <View                         style="@style/layout_horizontal"                         android:background="#aaaaaa"                         android:layout_weight="2"/>                        </LinearLayout>        <LinearLayout                style="@style/layout_vertical"                android:layout_weight="2"                android:orientation="vertical">                <View                         style="@style/layout_vertical"                         android:background="#ffffff"                         android:layout_weight="4"/>                         <View                         style="@style/layout_vertical"                         android:background="#aa0000"                         android:layout_weight="3"/>                 <View                         style="@style/layout_vertical"                         android:background="#00aa00"                         android:layout_weight="2"/>                 <View                         style="@style/layout_vertical"                         android:background="#0000aa"                         android:layout_weight="1"/>        </LinearLayout></LinearLayout>3.效果示範    ,分別為480*800介面和320*480介面。
二、自訂尺寸法    所謂自訂尺寸法,即為每一種螢幕介面定義一套介面尺寸大小。
1.res/values-480x320/dimen_height.xml功能:定義兩套應用於不同介面的尺寸dimen name="height_1_80">6dp</dimen>     <dimen name="height_3_80">18dp</dimen>   <dimen name="height_5_80">30dp</dimen> <dimen name="height_7_80">42dp</dimen> <dimen name="height_9_80">54dp</dimen> <dimen name="height_11_80">66dp</dimen> <dimen name="height_13_80">78px</dimen><dimen name="height_15_80">90px</dimen> <dimen name="height_17_80">102px</dimen>     ...........</resources>2.dimen_layout.xml功能:根據不同解析度螢幕,使用不同的尺寸<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical">                 <View                         android:layout_width="@dimen/width_76_80"                         android:layout_height="@dimen/height_10_80"                         android:background="#ffcccc"                         android:layout_margin="@dimen/width_2_80"/>                <LinearLayout                android:layout_width="fill_parent"                android:layout_height="fill_parent">                 <View                         android:layout_width="@dimen/width_30_80"                         android:layout_height="@dimen/height_50_80"                         android:background="#ccccff"                         android:layout_margin="@dimen/height_5_80"/>                 <LinearLayout                         android:layout_width="fill_parent"                         android:layout_height="fill_parent"                         android:orientation="vertical">                                 <Button                                 android:layout_width="@dimen/width_30_80"                                 android:layout_height="@dimen/height_5_80"                                 android:background="#ccffcc"                                 android:layout_marginBottom="@dimen/height_1_80"                                 android:text="5"/>                         <Button                                 android:layout_width="@dimen/width_30_80"                                 android:layout_height="@dimen/height_10_80"                                 android:background="#ccffcc"                                 android:layout_marginBottom="@dimen/height_1_80"                                 android:text="10"/>                         <Button                                 android:layout_width="@dimen/width_30_80"                                 android:layout_height="@dimen/height_15_80"                                 android:background="#ccffcc"                                 android:layout_marginBottom="@dimen/height_1_80"                                 android:text="15"/>                         <Button                                 android:layout_width="@dimen/width_30_80"                                 android:layout_height="@dimen/height_20_80"                                 android:background="#ccffcc"                                 android:text="20"/>                </LinearLayout>                </LinearLayout>                </LinearLayout>3.效果示範
三、多布局    所謂多布局,即為不同尺寸螢幕設計單獨的布局檔案。為了提供自適應不同螢幕的資源,我們可以為不同螢幕尺寸、不同螢幕解析度提供相應的布局資源、Drawable資源。需要注意的是,使用多布局方法,須在資訊清單檔中添加如下代碼,否則,會出現異常。
1.如何支援多個螢幕(1)在程式中顯示聲明你的應用程式支援螢幕尺寸的清單(AndroidMnifest.xml)

  1. <supports-screens   
  2.      android:resizeable=[“true”|”false”]  
  3.      android:smallScreens=[“true”|”false”]  
  4.      android:normalScreens=[“true”|”false”]  
  5.      android:largeScreens=[“true”|”false”]  
  6.      android:xlargeScreens=[“true”|”false”]  
  7.      android:anyDensity=[“true”|”false”]/>
2.文字自適應(1)dip: device independent pixels(裝置獨立像素). 不同裝置有不同的顯示效果,這個和裝置硬體有關,一般我們為了支援WVGA、HVGA和QVGA (WVGA=800x480,HVGA=480x320, QVGA=320x240)推薦使用這個,不依賴像素。(2)px: pixels(像素). 不同裝置顯示效果相同,一般我們HVGA代表320x480像素,這個用的比較多。 (3)pt: point,是一個標準的長度單位,1pt=1/72英寸,用於印刷業,非常簡單易用; (4)sp: scaled pixels(放大像素). 主要用於字型顯示best for textsize。可以根據使用者的字型大小喜好設定進行縮放     根據上面對單位的分析,使用sp為單位就可以實現自適應字型大小。在res檔案夾中建立一個檔案夾,叫做values-320x240。其中320x240是你手機螢幕的解析度,根據你手機螢幕的情況做不同的命名,例如values-800x480。在該檔案夾下建立一個dimens.xml檔案,定義各種字型的大小。那麼系統就會自動根據你手機螢幕的解析度去調用響應的檔案夾。         另外,值得提醒的是,記得在你預設的values檔案下的dimens.xml檔案中也要寫上相應的字型大小,因為當系統無法認識你手機螢幕大小的時候,它會自動去找你預設檔案中的東西,沒有寫的話程式會崩潰。 3.布局自適應(1)根據不同螢幕尺寸,提供不同布局    預設情況下面,android會自動調整應用程式的布局,但是大多數情況下面,根據廣義尺寸,小,正常,大,更大去增加不同的布局資源。比如,如果需要對大小為large的螢幕提供支援,需要在res目錄下建立一個檔案夾layout-large/並提供layout。當然,也可以在res目錄下建立layout-port和layout-land兩個目錄,裡面分別放置豎屏和橫屏兩種布局檔案,以適應對橫屏豎屏自動切換。layout :預設中等螢幕 layout-small :小螢幕 layout-large :大螢幕 layout-xlarge :特大螢幕 layout-land :橫屏 layout-port :豎屏註:Android3.2以上推薦使用在layout、values目錄添加-sw<N>dp,即螢幕尺寸至少寬N個dp才能使用該資源。(2)提供不同的螢幕密度和不同的位元影像drawables     預設情況下面系統會自動調整和縮放位元影像,但是難免展開位元影像,為了保證你的位元影像是最好看的,根據廣義密度,低,中型, 高,特高去添加不同的位元影像資源。比如,如需對密度為low的螢幕提供合適的圖片,需建立檔案夾drawable-ldpi/。應盡量使用點9格式的圖片,圖片大小的確定:low:medium:high:xhigh比例為3:4:6:8。舉例來說,對於中等密度(medium)的螢幕你的圖片像素大小為48×48,那麼低密度(low)螢幕的圖片大小應為36×36,高(high)的為72×72,extra high為96×96。Android系統支援多配置資源檔,我們可以追加新的資來源目錄到你的Android項目中。drawable-hdpi、drawable-mdpi、drawable-ldpi的區別:>drawable-hdpi裡面存放高解析度的圖片,如WVGA (480x800),FWVGA (480x854)  >drawable-mdpi裡面存放中等解析度的圖片,如HVGA (320x480)  >drawable-ldpi裡面存放低解析度的圖片,如QVGA (240x320) 
參考:http://www.eoeandroid.com/thread-173973-1-1.htmlhttp://blog.csdn.net/lonely_fireworks/article/details/7233575http://blog.csdn.net/kangyaping/article/details/7400280

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.