Android 多螢幕適配 dp和px的關係 最好用dp

來源:互聯網
上載者:User

標籤:9.png   general   ide   片段   out   dev   port   ica   複製   

Android 多螢幕適配 dp和px的關係

一直以來別人經常問我,android的多螢幕適配到底是怎麼弄,我也不知道如何講解清楚,或許自己也是挺迷糊。

以下得出的結論主要是結合官方文檔進行分析的https://developer.android.com/guide/practices/screens_support.html

android由於片段化太嚴重,而導致市面上出現非常多的種類尺寸手機裝置,當然也包括非常奇葩的解析度手機。所以我們在布局的時候使用px作為單位顯然不能很好的做到多螢幕的適配。其實在官方文檔中有介紹一種解決多螢幕適配的問題的尺寸單位dp/dip(Density-independent pixel )

文檔中也給出了px和dp之間的關聯運算式:px = dp * (dpi / 160) 其中dpi表示的是裝置螢幕密度,不同的裝置dpi可能不一致。

例如在一個dpi為240的裝置螢幕中,1個dp等價於2個px,也就是一個dp長度下包含2個px,如果dp與px的倍數越大,螢幕看起來也就越清晰。

Google已經針對dpi進行了分類:

  • A set of six generalized densities:
    • ldpi (low) ~120dpi
    • mdpi (medium) ~160dpi
    • hdpi (high) ~240dpi
    • xhdpi (extra-high) ~320dpi
    • xxhdpi (extra-extra-high) ~480dpi
    • xxxhdpi (extra-extra-extra-high) ~640dpi   

 上述表述可以看出他是一個區間的,並不是一個固定的值。

基於px和dp的關係式。假如在螢幕像素為800*1280,dpi為240的裝置上,dp的總長度只有600dp

在螢幕像素為800*1280 ,dpi為320的裝置上,dp的總長度只有400dp

以下是測試代碼,來驗證dp的實際長度

<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"    android:orientation="vertical"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#f00"        android:src="@drawable/ic_launcher" />    <ImageView        android:layout_width="100dip"        android:layout_height="40dip"        android:src="#0f0" />    <ImageView        android:layout_width="200dip"        android:layout_height="40dip"        android:src="#0fa" />    <ImageView        android:layout_width="300dip"        android:layout_height="40dip"        android:src="#0ff" />    <ImageView        android:layout_width="400dip"        android:layout_height="40dip"        android:src="#09f" /></LinearLayout>

720*1280  320dpi                        480*800   160dpi                                   480*800   240dpi   

dp總長度為360dp                           dp的總長度為480dp                                 dp的總長度為320dp

                           

總結一

一般情況下如果螢幕解析度和螢幕密度成一定比例的關係,那麼不管解析度多大,得出的dp總長度將會在一定的範圍內。那麼使用dp作為單位將能很好的解決多解析度適配的問題。

然而由於裝置螢幕解析度和螢幕密度並不是成一個固定的比例,所以全部用dp的寫,似乎不是很好。建議使用warp_content 和match_parent來配合使用。對於布局的高寬沒有明確限制的,盡量結合相對布局和權重來表示,這樣不管在什麼解析度下,介面的樣式不會錯亂。

 

Android 多螢幕適配 dp和px的關係 最好用dp

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.