閱讀538 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android屏幕自適應的四種方法

轉自:https://www.eoeandroid.com/home.php?mod=space&uid=636088&do=blog&id=48749

一、細說layout_weight

    目前最為推薦的Android多屏幕自適應解決方案。
    該屬性的作用是決定控件在其父布局中的顯示權重,一般用於線性布局中。其值越小,則對應的layout_width或layout_height的優先級就越高,一般橫向布局中,決定的是layout_width的優先級;縱向布局中,決定的是layout_height的優先級。
    傳統的layout_weight使用方法是將當前控件的layout_widthlayout_height都設置成fill_parent,這樣就可以把控件的顯示比例完全交給layout_weight;這樣使用的話,就出現了layout_weight越小,顯示比例越大的情況。不過對於2個控件還好,如果控件過多,且顯示比例也不相同的時候,控製起來就比較麻煩了,畢竟反比不是那麼好確定的。
    於是就有了現在最為流行的0px設值法。看似讓人難以理解的layout_height=0px的寫法,結合layout_weight,卻可以使控件成正比例顯示,輕鬆解決了當前Android開發最為頭疼的碎片化問題之一。
    先看下麵的stylesstyle_layout.xml

[代碼]php代碼:

01 <?xml version="1.0" encoding="utf-8"?>
02 <resources> 
03    
04 <!-- 全屏幕拉伸-->
05   <style name="layout_full"
06     <item name="android:layout_width">fill_parent</item> 
07     <item name="android:layout_height">fill_parent</item> 
08   </style>
09      
10 <!-- 固定自身大小-->
11   <style name="layout_wrap"
12     <item name="android:layout_width">wrap_content</item> 
13     <item name="android:layout_height">wrap_content</item> 
14   </style>
15    
16 <!-- 橫向分布-->
17   <style name="layout_horizontal" parent="layout_full"
18     <item name="android:layout_width">0px</item> 
19   </style>
20       
21 <!-- 縱向分布-->
22   <style name="layout_vertical" parent="layout_full"
23     <item name="android:layout_height">0px</item> 
24   </style>
25            
26 </resources>

 
可以看到,layout_widthlayout_height兩個屬性被我封裝成了4style
    根據實際布局情況,選用當中的一種,不需要自己設置,看過我前一個ActivityGroupDemo的同學應該非常熟悉了
    然後我的Demo的布局如下(weight_layout.xml

[代碼]php代碼:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
03         style="@style/layout_full"
04         android:orientation="vertical">
05         <LinearLayout 
06                 style="@style/layout_vertical"
07                 android:layout_weight="1"
08                 android:orientation="horizontal">
09                  <View
10                          style="@style/layout_horizontal"
11                          android:background="#aa0000"
12                          android:layout_weight="1"/>
13                  <View
14                          style="@style/layout_horizontal"
15                          android:background="#00aa00"
16                          android:layout_weight="4"/>
17                  <View
18                          style="@style/layout_horizontal"
19                          android:background="#0000aa"
20                          android:layout_weight="3"/>
21                  <View
22                          style="@style/layout_horizontal"
23                          android:background="#aaaaaa"
24                          android:layout_weight="2"/>                 
25         </LinearLayout> 
26         <LinearLayout 
27                 style="@style/layout_vertical"
28                 android:layout_weight="2"
29                 android:orientation="vertical">
30                 <View
31                          style="@style/layout_vertical"
32                          android:background="#ffffff"
33                          android:layout_weight="4"/>        
34                  <View
35                          style="@style/layout_vertical"
36                          android:background="#aa0000"
37                          android:layout_weight="3"/>
38                  <View
39                          style="@style/layout_vertical"
40                          android:background="#00aa00"
41                          android:layout_weight="2"/>
42                  <View
43                          style="@style/layout_vertical"
44                          android:background="#0000aa"
45                          android:layout_weight="1"/>
46    
47         </LinearLayout>
48 </LinearLayout>
二、自定義尺寸法
    這個是我自己想出來的方法,可能是個比較笨的方法,所以沒有多少人提過用這種方法解決自適應的問題。雖然這個方法缺點也很多,但有時候也是個不錯的方法。
    先看下麵這張圖


     可以看到我定義了兩套尺寸文件,我們可以看下其中一個文件

[代碼]php代碼:

01 <?xml version="1.0" encoding="utf-8"?>
02 <resources>
03 <dimen name="height_1_80">6px</dimen><dimen name="height_2_80">12px</dimen>
04 <dimen name="height_3_80">18px</dimen><dimen name="height_4_80">24px</dimen>
05 <dimen name="height_5_80">30px</dimen><dimen name="height_6_80">36px</dimen>
06 <dimen name="height_7_80">42px</dimen><dimen name="height_8_80">48px</dimen>
07 <dimen name="height_9_80">54px</dimen><dimen name="height_10_80">60px</dimen>
08 <dimen name="height_11_80">66px</dimen><dimen name="height_12_80">72px</dimen>
09 <dimen name="height_13_80">78px</dimen><dimen name="height_14_80">84px</dimen>
10 <dimen name="height_15_80">90px</dimen><dimen name="height_16_80">96px</dimen>
11 <dimen name="height_17_80">102px</dimen><dimen name="height_18_80">108px</dimen>
12 <dimen name="height_19_80">114px</dimen><dimen name="height_20_80">120px</dimen>
13 <dimen name="height_21_80">126px</dimen><dimen name="height_22_80">132px</dimen>
14 <dimen name="height_23_80">138px</dimen><dimen name="height_24_80">144px</dimen>
15 <dimen name="height_25_80">150px</dimen><dimen name="height_26_80">156px</dimen>
16 <dimen name="height_27_80">162px</dimen><dimen name="height_28_80">168px</dimen>
17 <dimen name="height_29_80">174px</dimen><dimen name="height_30_80">180px</dimen>
18 <dimen name="height_31_80">186px</dimen><dimen name="height_32_80">192px</dimen>
19 <dimen name="height_33_80">198px</dimen><dimen name="height_34_80">204px</dimen>
20 <dimen name="height_35_80">210px</dimen><dimen name="height_36_80">216px</dimen>
21 <dimen name="height_37_80">222px</dimen><dimen name="height_38_80">228px</dimen>
22 <dimen name="height_39_80">234px</dimen><dimen name="height_40_80">240px</dimen>
23 <dimen name="height_41_80">246px</dimen><dimen name="height_42_80">252px</dimen>
24 <dimen name="height_43_80">258px</dimen><dimen name="height_44_80">264px</dimen>
25 <dimen name="height_45_80">270px</dimen><dimen name="height_46_80">276px</dimen>
26 <dimen name="height_47_80">282px</dimen><dimen name="height_48_80">288px</dimen>
27 <dimen name="height_49_80">294px</dimen><dimen name="height_50_80">300px</dimen>
28 <dimen name="height_51_80">306px</dimen><dimen name="height_52_80">312px</dimen>
29 <dimen name="height_53_80">318px</dimen><dimen name="height_54_80">324px</dimen>
30 <dimen name="height_55_80">330px</dimen><dimen name="height_56_80">336px</dimen>
31 <dimen name="height_57_80">342px</dimen><dimen name="height_58_80">348px</dimen>
32 <dimen name="height_59_80">354px</dimen><dimen name="height_60_80">360px</dimen>
33 <dimen name="height_61_80">366px</dimen><dimen name="height_62_80">372px</dimen>
34 <dimen name="height_63_80">378px</dimen><dimen name="height_64_80">384px</dimen>
35 <dimen name="height_65_80">390px</dimen><dimen name="height_66_80">396px</dimen>
36 <dimen name="height_67_80">402px</dimen><dimen name="height_68_80">408px</dimen>
37 <dimen name="height_69_80">414px</dimen><dimen name="height_70_80">420px</dimen>
38 <dimen name="height_71_80">426px</dimen><dimen name="height_72_80">432px</dimen>
39 <dimen name="height_73_80">438px</dimen><dimen name="height_74_80">444px</dimen>
40 <dimen name="height_75_80">450px</dimen><dimen name="height_76_80">456px</dimen>
41 <dimen name="height_77_80">462px</dimen><dimen name="height_78_80">468px</dimen>
42 <dimen name="height_79_80">474px</dimen><dimen name="height_80_80">480px</dimen>  
43 </resources>

 
    這個是values-480x320文件夾下dimens_height.xml文件中的代碼,我把整個高度分成了80等分,這是因為大部分屏幕的寬度或高度都是80的整數倍(個別特殊的除外),不同的等分在不同的分辨率中設定不同的尺寸值。
    由於每一套界麵都要寫一套,所以有些同學可能覺著不太好,不過這個寫起來比較簡單,而且以後也不用改,所以有時候也可以考慮用一下!
    再看我Demo的布局代碼(dimen_layout.xml

[代碼]php代碼:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
03         android:layout_width="fill_parent"
04          android:layout_height="fill_parent"
05         android:orientation="vertical">
06                  <View
07                          android:layout_width="@dimen/width_76_80"
08                          android:layout_height="@dimen/height_10_80"
09                          android:background="#ffcccc"
10                          android:layout_margin="@dimen/width_2_80"/>       
11         <LinearLayout
12                  android:layout_width="fill_parent"
13                  android:layout_height="fill_parent">
14                  <View
15                          android:layout_width="@dimen/width_30_80"
16                          android:layout_height="@dimen/height_50_80"
17                          android:background="#ccccff"
18                          android:layout_margin="@dimen/height_5_80"/>
19                  <LinearLayout
20                          android:layout_width="fill_parent"
21                          android:layout_height="fill_parent"
22                          android:orientation="vertical">       
23                          <Button
24                                  android:layout_width="@dimen/width_30_80"
25                                  android:layout_height="@dimen/height_5_80"
26                                  android:background="#ccffcc"
27                                  android:layout_marginBottom="@dimen/height_1_80"
28                                  android:text="5"/>
29                          <Button
30                                  android:layout_width="@dimen/width_30_80"
31                                  android:layout_height="@dimen/height_10_80"
32                                  android:background="#ccffcc"
33                                  android:layout_marginBottom="@dimen/height_1_80"
34                                  android:text="10"/>
35                          <Button
36                                  android:layout_width="@dimen/width_30_80"
37                                  android:layout_height="@dimen/height_15_80"
38                                  android:background="#ccffcc"
39                                  android:layout_marginBottom="@dimen/height_1_80"
40                                  android:text="15"/>
41                          <Button
42                                  android:layout_width="@dimen/width_30_80"
43                                  android:layout_height="@dimen/height_20_80"
44                                  android:background="#ccffcc"
45                                  android:text="20"/>
46                  </LinearLayout>       
47          </LinearLayout>               
48 </LinearLayout>

 

    以上是我寫的統一的布局代碼,來看下在兩個不同分辨率的模擬器上的顯示效果吧(大家注意我的代碼中有margin這樣的值也用到了自定義尺寸,如果這個margin使用layout_weight來控製的話,無疑要多嵌套一層線性布局,所以說沒有一個方法是十全十美的,這第2個方法有時候用起來反而還要方便一些)





三、java代碼中設置寬高度
    也許很多人會反對這種方法,因為即使是官方也是推薦使用xml的方式寫布局。不過我們在這不會像Swing那樣寫那麼多麻煩的布局代碼,因為我們隻是在代碼中重新設定控件的寬高度而已,其他屬性依然是交給xml布局文件的。這個方法其實是我跟同事偷學來的,雖然我不讚成這樣的方法,但他確確實實也是解決屏幕自適應問題的方案之一,而且它沒我想象的那麼複雜,其實很簡單。
    首先我們要做的是獲取當前屏幕的寬高度,因為這個在後麵要用到
    我們可以寫兩個靜態變量用來保存當前屏幕的寬高度:

[代碼]php代碼:

1 ic class Constant {
2         public static int displayWidth;  //屏幕寬度
3         public static int displayHeight; //屏幕高度
4 }


    然後在第一個Activity啟動的時候,獲取這兩個值

[代碼]php代碼:

1 DisplayMetrics displayMetrics = new DisplayMetrics();
2         getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
3         Constant.displayWidth = displayMetrics.widthPixels;
4         Constant.displayHeight = displayMetrics.heightPixels;


    布局代碼我們可以全都統一寫成wrap-content,其實寫成什麼都無所謂,因為這個值隻是暫時的

[代碼]php代碼:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent">
06 <Button 
07         android:id="@+id/btn1"
08     android:layout_width="wrap_content"
09     android:layout_height="wrap_content"
10     android:background="#ffcccc"
11     android:text="aaaaaaaa"/>
12 <Button 
13         android:id="@+id/btn2"
14     android:layout_width="wrap_content"
15     android:layout_height="wrap_content"
16     android:background="#ccffcc"
17     android:text="bbbbbbbbb"/>
18 <Button 
19         android:id="@+id/btn3"
20     android:layout_width="wrap_content"
21     android:layout_height="wrap_content"
22     android:background="#ccccff"
23     android:text="ccccccccc"/>
24 <Button 
25         android:id="@+id/btn4"
26     android:layout_width="wrap_content"
27     android:layout_height="wrap_content"
28     android:background="#ffffcc"
29     android:text="dddddddddddddddddd"/>  
30 </LinearLayout>

 
    最後我們在ActivityonCreate方法裏這麼做

[代碼]php代碼:

01 // 第一個按鈕,寬度100%,高度10%
02                 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
03                                 LayoutParams.FILL_PARENT,
04                                 (int) (Constant.displayHeight * 0.1f + 0.5f));
05                 btn1.setLayoutParams(params);
06                 // 第二個按鈕,寬度100%,高度30%
07                 LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
08                                 LayoutParams.FILL_PARENT,
09                                 (int) (Constant.displayHeight * 0.3f + 0.5f));
10                 btn2.setLayoutParams(params2);
11                 // 第三個按鈕,寬度50%,高度20%
12                 LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
13                                 (int) (Constant.displayWidth * 0.5f + 0.5f),
14                                 (int) (Constant.displayHeight * 0.2f + 0.5f));
15                 btn3.setLayoutParams(params3);
16                 // 第三個按鈕,寬度70%,高度填滿剩下的空間
17                 LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
18                                 (int) (Constant.displayWidth * 0.7f + 0.5f),
19                                 LayoutParams.FILL_PARENT);
20                 btn4.setLayoutParams(params4);

 
    大家可以看到其實代碼並不複雜,都能看得懂



多布局
    做為最後的方法,也是最後一個才會考慮的方法,那就是為不同的尺寸界麵單獨寫布局。不到萬不得已不要用這個方法,相信不少人和我一樣都被逼著用過這個方法吧。需要說明的是,橫豎屏切換使用不同布局也是用這個方法解決的;代碼我就不上了,給大家看兩張圖吧,一個是1個布局的,一個是寫了多布局的,大家一看就明白了
補充一下,寫多個布局的時候,配置文件一定要加上這段配置代碼,不然有時可能會出問題
    <supports-screens android:largeScreens="true"
                android:normalScreens="true" android:anyDensity="true" />



五、其他
    以上說的都是多個屏幕顯示相同內容需要考慮的問題,還有一種是在不同的屏幕上顯示內容不同的情況,其實這個問題我們往往是用滾動視圖來解決的,也就是ScrowView;需要注意的是ScrowView中使用layout_weight是無效的,既然使用ScrowView了,就把它裏麵的控件的大小都設成固定的吧。
    此外關於圖片的自適應問題,主要是2點,一個是9patch圖,這個東西大家都要學會去做,不難;不過有些編譯器在識別9patch圖時會出這樣那樣的bug,像我的Eclipse就不認這個,而同一個9patch圖在別的電腦上卻是沒問題的,
    第二個要說的是我曾經被困擾的一個問題,對於480x800  480x854這兩個尺寸,他們顯示同一個圖片時,總有一個會拉伸(如果9patch可以解決的還好)。其實當初困擾我的是,這兩個尺寸都是hdpi的,以為無法給這兩個屏幕做不同的圖片。後來無意中發現,圖片可以和布局一樣分多個尺寸的,而不僅僅是根據密度分,也就是說你可以寫這樣的文件夾drawable-hdpi-800x480drawable-hdpi-854x480,在它們裏麵放不同的圖片,這樣圖片也能自適應了。

最後更新:2017-04-03 07:57:02

  上一篇:go EA強大的畫圖工具---設計數據庫表格
  下一篇:go android UI設計推薦