1.用layout_weight實現自適應螢幕
一、細說layout_weight 目前最為推薦的Android多螢幕自適應解決方案。 該屬性的作用是決定控制項在其父布局中的顯示權重,一般用於線性布局中。其值越小,則對應的layout_width或layout_height的優先順序就越高,一般橫向布局中,決定的是layout_width的優先順序;縱向布局中,決定的是layout_height的優先順序。 傳統的layout_weight使用方法是將當前控制項的layout_width和layout_height都設定成fill_parent,這樣就可以把控制項的顯示比例完全交給layout_weight;這樣使用的話,就出現了layout_weight越小,顯示比例越大的情況。不過對於2個控制項還好,如果控制項過多,且顯示比例也不相同的時候,控制起來就比較麻煩了,畢竟反比不是那麼好確定的。
於是就有了現在最為流行的
0px設值法。看似讓人難以理解的layout_height=0px的寫法,結合layout_weight,卻可以使控制項成正比例顯示,輕鬆解決了當前Android開發最為頭疼的片段化問題之一。 先看下面的styles(style_layout.xml)
?
程式碼片段,雙擊複製
0102030405060708091011121314151617181920212223242526 |
<?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_width和layout_height兩個屬性被我封裝成了4個style 根據實際布局情況,選用當中的一種,不需要自己設定,看過我前一個ActivityGroup的Demo的同學應該非常熟悉了 然後我的Demo的布局如下(weight_layout.xml)
?
程式碼片段,雙擊複製
010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748 |
<?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.Timer and TimerTask 來定時更新一個程式
Timer timer = new Timer("");
timer.schedule(new TimerTask(){},Date date ,long peroid);
Date表示要在什麼時間運行,peroid表示每隔一段時間之後就會運行一次,這裡如果只想運行一次的話,就不要定義 long peroid 參數了。
3.ANR --- 程式不反應
這個會在 data/anr/ trace .txt 中,我們可以分析這個檔案來看一下為什麼會出現這個問題,如所示:
包括了出現的時間,以及在哪個包中出現的,有在 哪個 .java檔案,這樣就很容易定位。看一下是不是 IO 記憶體泄露 堵塞