日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android资源适配

發布時間:2025/6/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android资源适配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android程序在不同的環境(如分辨率不同的機型,不同語言的操作系統)或狀態(如橫豎屏)下,需要不同的資源(如圖片,語言),一般情況下,這些資源是自適配的,只需要命名不同的資源文件夾,把同名的資源放入這些文件夾中即可

? 比如下面就是一個程序項目的資源文件夾:

?



不同的命名代表什么含義呢?詳細見下表:

限定符Qualifier

Values

移動國家碼MCC和移動網絡碼MNC

手機設備SIM卡上的移動國家碼和移動網絡碼。比如mcc310-mnc004?(美國,Verizon品牌);?mcc208-mnc00?(法國,Orange品牌);?mcc234-mnc00?(英國,BT品牌).?
如果這個設備使用一個無線連接(GSM電話),則MCC來自SIM卡,而MNC來自該設備將要附著的網絡。你有時會僅使用MCC,例如包含特定國家合法資源在您的應用程序中。如果您的應用程序指定了MCC/MNC組合的資源,這些資源僅在MCCMNC都匹配的時候才能使用。

語言和區域Languageand region

兩個字母的ISO639-1語言碼和ISO3166-1-alpha-2區域碼?("r"為前綴)。比如en-rUS,fr-rFR,es-rES.這個代碼是大小寫敏感的:語言碼是小寫字母,國家碼是大寫字母。你不能單獨指定一個區域,但是你可以單獨指定一個語言,比如en,fr, es, zh.

屏幕方向Screenorientation

縱向,橫向,正方形(port,land, square

屏幕像素密度Screenpixel density

92dpi,108dpi.?Android選擇使用哪個資源時,它對屏幕像素密度的處理和其它限定符不同。在文章后面描述的步驟1Android如何查找最匹配的目錄中,屏幕密度總被認為是匹配的。在步驟4中,如果被考慮的限定符是屏幕密度,Android將選擇在那個位置的最佳匹配,而無需繼續步驟5

觸摸屏類型Touchscreentype

非觸摸式,觸摸筆,手指(notouch,stylus, finger

鍵盤可用方式Whetherthe keyboard is available to the user

外在鍵盤,隱藏鍵盤,軟鍵盤(keysexposed,keyshidden,keyssoft
如果你的應用程序有一個特定的資源只能通過軟件盤使用,則使用keyssoft?值,如果沒有keyssoft?資源可用(只有keysexposed??keyshidden)并且該設備顯示了一個軟鍵盤,那么系統將使用keysexposed?資源。

首選文本輸入方法Primarytext input method

不支持按鍵,標準鍵盤,12(nokeys,qwerty, 12key)

首選非觸摸式導航方法Primarynon-touchscreen
navigation method

不支持導航,滑板,跟蹤球,滾輪(nonav,dpad, trackball, wheel)

屏幕分辨率Screendimensions

320x240,640x480,?.?更大的分辨率必須先被指定。

SDK版本SDKversion

設備支持的SDK版本,比如v3Android1.0SDKv11.1SDKv21.5SDKv3

小版本(Minorversion)

你目前還不能指定小版本,它總是被設置為0

文件夾命名的各個部分是有順序的。即按上表從上到下的優先級別排列,如:

drawable-en-rUS-land-mdpi
drawable-en-rUS-port-160dpi-finger-qwerty-dpad-480x320/(US代表美式英語)

舉個使用過程中的例子:

任意建一個android項目。看一下res目錄結構:


drawable-hdpi-finger與drawable-ldpi目錄下面兩張圖片名稱一樣,但是圖片不是同一張,分別是:


layout、layout-finger、layout-land-finger下面的main.xml文件內容分別是:

layout/main.xml

[html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????>??
  • ??
  • <ImageView??
  • ????android:layout_width="wrap_content"??
  • ????android:layout_height="wrap_content"??
  • ????android:src="@drawable/icon"??
  • ????/>???
  • <Button??
  • ????android:layout_width="wrap_content"??
  • ????android:layout_height="wrap_content"??
  • ????android:background="@drawable/icon"??
  • ????/>????
  • <TextView???
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????android:text="layout?only"/>???
  • </LinearLayout>??
  • layout-finger/main.xml

    [html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????>??
  • ????<ImageView??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:src="@drawable/icon"??
  • ????????/>???
  • ????<Button??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:background="@drawable/icon"??
  • ????????/>???
  • ????<TextView???
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="fill_parent"??
  • ????????android:text="layout-finger"/>????
  • </LinearLayout>??
  • layout-land-finger/main.xml

    [html]?view plaincopy
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:orientation="vertical"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????>??
  • ????<ImageView??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:src="@drawable/icon"??
  • ????????/>???
  • ????<Button??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:background="@drawable/icon"??
  • ????????/>???
  • ????<TextView???
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="fill_parent"??
  • ????????android:text="layout-land-finger"/>????
  • </LinearLayout>??
  • 可以看出,他們區別在于TextView的顯示內容不同,其它的一樣。

    運行App,在模擬器上顯示效果


    切換模擬器屏幕方向:


    可以看出,顯示的圖片始終是,而不是。說明帶有finger的文件夾優先級別較高。

    layout-land-finger:適合電容屏、電阻屏以及非觸摸屏的設備并且該設備是水平方向。

    layout-finger:適合電容屏、電阻屏以及非觸摸屏的設備但不要求是水平方向。

    layout:適合電阻屏以及非觸摸屏的設備。

    另外,圖片資源應該按照l:m:h =?3:4:6 的比例來設計,就是如果你只準備適合一種像素的drawable文件夾,如drawable-hdpi,并不是在低像素密度的手機上就不會顯示圖片或者報錯的問題。因為資源名稱是惟一的,在低像素密度手機上照樣可以顯示圖片資源,只不過不好看而已,因為系統會自動調整hdi的大小來適配,如果系統想找一個ldpi(低密度)的資源,但找不到。那么系統會等比縮小hdpi的資源,為什么不尋找mdpi呢?因為系統對于hdpi更容易縮放,它的系數為0.5,相比mdpi的0.75來說。0.5的的性價比更高(方便計算,正確率也高一點)。關于這一點最好的證明就是android版本的QQ瀏覽器。你解壓后發現它關于drawable就只有drawable和drawable-hdpi兩個文件夾,看來騰訊里android的開發者應該很熟悉android系統規律了,因為默認的drawable 就是mdpi,只需要定義一套hdpi的drawable資源就行了。

    當然,如果你想保證一些資源文件保持原樣,尤其是圖片不被拉伸或壓縮,可以放入drawable-nodpi這個目錄下

    總結

    以上是生活随笔為你收集整理的android资源适配的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。