Android:屏幕自適應

demo下載:http://www.eoeandroid.com/forum.php?mod=attachment&aid=NjE0Njh8ZTIyZDA2M2N8MTMzODgyOTQxN3w1NzAwOTV8MTczOTczphp

 

如下是Demo首頁的預覽圖java

1、細說layout_weightandroid

    目前最爲推薦的Android多屏幕自適應解決方案。佈局

    該屬性的做用是決定控件在其父佈局中的顯示權重,通常用於線性佈局中。其值越小,則對應的layout_width或layout_height的優先級就越高,通常橫向佈局中,決定的是layout_width的優先級;縱向佈局中,決定的是layout_height的優先級。spa

    傳統的layout_weight使用方法是將當前控件的layout_widthlayout_height都設置成fill_parent,這樣就能夠把控件的顯示比例徹底交給layout_weight;這樣使用的話,就出現了layout_weight越小,顯示比例越大的狀況。不過對於2個控件還好,若是控件過多,且顯示比例也不相同的時候,控制起來就比較麻煩了,畢竟反比不是那麼好肯定的。code

    因而就有了如今最爲流行的0px設值法。看似讓人難以理解的layout_height=0px的寫法,結合layout_weight,卻可使控件成正比例顯示,輕鬆解決了當前Android開發最爲頭疼的碎片化問題之一。orm

    先看下面的stylesstyle_layout.xmlxml

複製代碼

<?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>

複製代碼

能夠看到,layout_widthlayout_height兩個屬性被我封裝成了4style圖片

    根據實際佈局狀況,選用當中的一種,不須要本身設置,看過我前一個ActivityGroupDemo的同窗應該很是熟悉了ip

    而後個人Demo的佈局以下(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>

複製代碼

整個界面佈局看起來很是直觀,只是嵌套的邏輯要本身理下。顯示效果以下圖,其中左面一個是480x800的界面,右面的是320x480的界面(後面的圖也如此),能夠看出顯示比例和代碼中徹底一致,我就很少說了,你們對照下就能看出來了。

 

2、自定義尺寸法

 不推薦已刪除

3、java代碼中設置寬高度

    也許不少人會反對這種方法,由於即便是官方也是推薦使用xml的方式寫佈局。不過咱們在這不會像Swing那樣寫那麼多麻煩的佈局代碼,由於咱們只是在代碼中從新設定控件的寬高度而已,其餘屬性依然是交給xml佈局文件的。這個方法實際上是我跟同事偷學來的,雖然我不同意這樣的方法,但他確確實實也是解決屏幕自適應問題的方案之一,並且它沒我想象的那麼複雜,其實很簡單。

    首先咱們要作的是獲取當前屏幕的寬高度,由於這個在後面要用到

    咱們能夠寫兩個靜態變量用來保存當前屏幕的寬高度:

public class Constant {
        public static int displayWidth;  //屏幕寬度        
        public static int displayHeight; //屏幕高度
        }

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

 

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

 

 佈局代碼咱們能夠全都統一寫成wrap-content,其實寫成什麼都無所謂,由於這個值只是暫時的 

複製代碼

<?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">
<Button  
        android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffcccc"
    android:text="aaaaaaaa"/>
<Button  
        android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ccffcc"
    android:text="bbbbbbbbb"/>
<Button  
        android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ccccff"
    android:text="ccccccccc"/>
<Button  
        android:id="@+id/btn4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#ffffcc"
    android:text="dddddddddddddddddd"/>   
</LinearLayout>

複製代碼

最後咱們在ActivityonCreate方法裏這麼作 

複製代碼

// 第一個按鈕,寬度100%,高度10%
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                (int) (Constant.displayHeight * 0.1f + 0.5f));
                btn1.setLayoutParams(params);
                // 第二個按鈕,寬度100%,高度30%
                LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                (int) (Constant.displayHeight * 0.3f + 0.5f));
                btn2.setLayoutParams(params2);
                // 第三個按鈕,寬度50%,高度20%
                LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
                                (int) (Constant.displayWidth * 0.5f + 0.5f),
                                (int) (Constant.displayHeight * 0.2f + 0.5f));
                btn3.setLayoutParams(params3);
                // 第三個按鈕,寬度70%,高度填滿剩下的空間
                LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
                                (int) (Constant.displayWidth * 0.7f + 0.5f),
                                LayoutParams.FILL_PARENT);
                btn4.setLayoutParams(params4);

複製代碼

你們能夠看到其實代碼並不複雜,都能看得懂

    下面是效果顯示圖

多佈局

    作爲最後的方法,也是最後一個纔會考慮的方法,那就是爲不一樣的尺寸界面單獨寫佈局。不到萬不得已不要用這個方法,相信很多人和我同樣都被逼着用過這個方法吧。須要說明的是,橫豎屏切換使用不一樣佈局也是用這個方法解決的;代碼我就不上了,給你們看兩張圖吧,一個是1個佈局的,一個是寫了多佈局的,你們一看就明白了

補充一下,寫多個佈局的時候,配置文件必定要加上這段配置代碼,否則有時可能會出問題
   

 <supports-screens android:largeScreens="true"
                android:normalScreens="true" android:anyDensity="true" />



<ignore_js_op>4-2.png

5、其餘

    以上說的都是多個屏幕顯示相同內容須要考慮的問題,還有一種是在不一樣的屏幕上顯示內容不一樣的狀況,其實這個問題咱們每每是用滾動視圖來解決的,也就是ScrowView;須要注意的是ScrowView中使用layout_weight是無效的,既然使用ScrowView了,就把它裏面的控件的大小都設成固定的吧。

    此外關於圖片的自適應問題,主要是2點,一個是9patch圖,這個東西你們都要學會去作,不難;不過有些編譯器在識別9patch圖時會出這樣那樣的bug,像個人Eclipse就不認這個,而同一個9patch圖在別的電腦上倒是沒問題的,

    第二個要說的是我曾經被困擾的一個問題,對於480x800  480x854這兩個尺寸,他們顯示同一個圖片時,總有一個會拉伸(若是9patch能夠解決的還好)。其實當初困擾個人是,這兩個尺寸都是hdpi的,覺得沒法給這兩個屏幕作不一樣的圖片。後來無心中發現,圖片能夠和佈局同樣分多個尺寸的,而不只僅是根據密度分,也就是說你能夠寫這樣的文件夾drawable-hdpi-800x480drawable-hdpi-854x480,在它們裏面放不一樣的圖片,這樣圖片也能自適應了。

相關文章
相關標籤/搜索